Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a promise-then chain using map or reduce on an arbitrary number of chain elements?

I'm stuck on the following:

A script is returning an arbitrary number n or array, like this:

[["a"], ["b"], ["c"], ["d"]]

I need to loop over the arrays using promise then(), but as I don't know how many elements there will be, I have ended up doing this:

  var bundle_list = [["a"], ["b"], ["c"], ["d"]];

  var x = bundle_list.reduce(function(current, next) {
  console.log(current);

  // requestBundle will also return a promise
  return requestBundle(current)
    .then(function(bundle_response) {
      // do foo
      console.log("CALLING NEXT")
      console.log(next);
      return RSVP.resolve(next);
    });
})

x.then(function(last_response) {
  return console.log("DONE")
});

My problem is that my reduce/map both trigger all iterations before my async code is run, so I'm getting 3x the current console followed by the done console. So all my map "loop" run instantly, with the results clocking in a little (too) later...

I'm using this RSVP implementation, but it's A+ so should not be a problem. I have been trying to work along the answer provided here, but I cannot get it to work properly.

Question:
Is it possible to create a "then-chain" with an arbitrary number of then statements. If so, some pointers are appreciated!

Thanks!

like image 773
frequent Avatar asked May 04 '14 00:05

frequent


People also ask

Can promise be chained?

Introduction to the JavaScript promise chainingThe callback passed to the then() method executes once the promise is resolved. In the callback, we show the result of the promise and return a new value multiplied by two ( result*2 ).

How do you resolve a promise then?

Promise resolve() method: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.

What is chaining of promise?

Promise chaining: Promise chaining is a syntax that allows you to chain together multiple asynchronous tasks in a specific order. This is great for complex code where one asynchronous task needs to be performed after the completion of a different asynchronous task.


1 Answers

A for (or forEach) loop should do:

var queue = RSVP.Promise.resolve(); // in ES6 or BB, just Promise.resolve();

bundle_list.forEach(function(el){
    queue = queue.then(function(res){
        console.log("Calling async func for", el);
        return requestBundle(el);
    });
});

queue.then(function(lastResponse){
    console.log("Done!");
});
like image 162
Benjamin Gruenbaum Avatar answered Oct 17 '22 10:10

Benjamin Gruenbaum