Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

javascript - 如何执行一个异步函数,该函数需要使用数组中的reduce函数来将参数从数组中传入promise(how to execute an async functions which neds to take arguments from array in to an promise which use Reduce function Javascript)

I'm new to javascript and Promises, Here is the array:

(我是javascript和Promises的新手,这里是数组:)

resultSet: [
{loanID : "LN111", branch:"ABC", status:"APPROVED"},
{loanID : "LN222", branch:"ABC", status:"REJECTED"},
{loanID : "LN333", branch:"ABC", status:"APPROVED"}
]

I have two functions each take input argument as an object with status value either 'APPROVED' and 'REJECTED' and it's loanID

(我有两个函数,每个函数都将输入参数作为状态值为“ APPROVED”和“ REJECTED”的对象,并且它的借贷标识为)

function A : return functionApprove(resultSet) --> this one takes object which has status = 'APPROVED'
function B : return functionReject(resultSet) --> this one takes object which has status = 'REJECTED'

How do sequentially execute this two functions by using Reduce by looping over each element in resultSet.

(如何通过在resultSet中的每个元素上循环使用Reduce来依次执行这两个函数。)

So far my try is below:

(到目前为止,我的尝试如下:)

function ProcessRecords(){
    for (i = 0; i < resultSet.length; i++){
        if (resultSet[i].status = "APPROVED"){
            return functionApprove(resultSet[i].loanID)
                   .then (function(response){"call another function do some updates with the response"});
        else {
             return functionReject(resultSet[i].loanID)
                   .then (function(response){"call another function do some updates with the response"});
        };
  ask by Mar1009 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...