0

I have a set of APIs which I have to run inside a for loop

for(var i=0; i<array.length; i++ )
    service.getfunction(array[i]).then(function(response) {
      service.getfunction1(response).then(function(response) {
        service.getfunction2(response).then(function(response) {
          service.getfunction3(response).then(function(response) {
             console.log(response);
          });
        });
      });
    });
)

Second loop should start only after I got result from last getfunction3 API for first loop. How can I do this?

3 Answers 3

2

First of all - you can chain your promises like:

function doJob(i) {
  return service.getfunction(array[i]).then(function(response) {
    return service.getfunction1(response);
  }).then(function(response) {
    return service.getfunction2(response);
  }).then(function(response) {
    return service.getfunction3(response);
  }).then(function(response) {
    console.log(response);
  });
}

This function will return promise that will be resolved once all this service calls done. And now lets use it:

var p = Promise.resolve(true);
for(var i = 0; i < array.length; i++) {
  (function(idx) { //We need to wrap it or we will pass incorrect i into doJob
    p = p.then(function() {
      return doJob(idx);
    });
  })(i);
}
p.then(function() {
  console.log('Everything done');
});
Sign up to request clarification or add additional context in comments.

3 Comments

This can be alot simplified by omitting the return service.getfunctionX(response). The then will by default return the resolved value from the previous promise, thus you can simply write service.getFunction(array[i]).then(service.getfunction1).then(service.getfunction2).then(service.getfunction3)...
What if any of the API got failed?
@Anna you can use second argument of then function to handle errors, or chain catch function. It will get all error from whole chain after last catch.
0

Wrap it in a function:

// Declare function 
function LoopAPI() {
    for(var i=0; i<array.length; i++ )
        service.getfunction(array[i]).then(function(response) {
          service.getfunction1(response).then(function(response) {
            service.getfunction2(response).then(function(response) {
              service.getfunction3(response).then(function(response) {
                 console.log(response);

                 // Call function again
                 LoopAPI();
              });
            });
          });
        });
    )
}

// First call
LoopAPI();

Comments

0

You can chain promises then use Array.reduce to call them in order as such:

array.reduce(function(previous, next)
{
    return previous.then(function()
    {
        return service.getfunction(next);
    })
    .then(function(response)
    {
        return service.getfunction1(response);
    })
    .then(function(response)
    {
        return service.getfunction2(response);
    })
    .then(function(response)
    {
        return service.getfunction3(response);
    });
}, Promise.resolve())
.then(function()
{
    console.log("All done.");
})
.catch(function(error)
{
    console.log(error);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.