Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluebird mapSeries

I am trying to execute a series of promises in order, only going to the next one after the previous is resolved. From Bluebird docs:

The iterator won't be called for an item until its previous item, and the promise returned by the iterator for that item are fulfilled. http://bluebirdjs.com/docs/api/promise.mapseries.html

var Promise = require('bluebird');


function test(time) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            console.log(time);
            resolve(time);
        }, time);
    });
}

Promise.mapSeries([test(2000), test(1000), test(500), test(3000)], function(a) {
    return a;
}).then(function(results) {
    console.log(results);
});

What I expect is that the console.log inside the test function to show: 2000, 1000, 500, 3000 in that order. I expect that because as the docs states, each item goes only after the previous is resolved. Instead, I get 500, 1000, 2000, 3000, which reflects that all the functions are called instanstaneously. Moreover, results does show the results in the order they were called, though that is irrelevant at this point.

Am I misunderstanding something here?

like image 807
yBrodsky Avatar asked Mar 03 '17 15:03

yBrodsky


People also ask

What is promise mapSeries?

Call an iterator function for each element of an array in series, ensuring that no iterator is called before the promise returned by the previous iterator is fulfilled, in effect preventing parallel execution. Like async. mapSeries, but for promises.

How does promise map work?

map. Given a finite Iterable (arrays are Iterable ), or a promise of an Iterable , which produces promises (or a mix of promises and values), iterate over all the values in the Iterable into an array and map the array to another using the given mapper function.

What is Bluebird promise?

Bluebird JS is a fully-featured Promise library for JavaScript. The strongest feature of Bluebird is that it allows you to “promisify” other Node modules in order to use them asynchronously. Promisify is a concept applied to callback functions.

What is promise each?

each. Given an Iterable (an array, for example), or a promise of an Iterable , iterates serially over all the values in it, executing the given iterator on each element.


1 Answers

Your test calls are being before Promise.mapSeries ever gets a chance to run. Also mapSeries is normally run on a promise instance. Maybe the following example helps to understand? Note how test(time) this time returns a function.

function test(time) {
    return function(value) {
      return new Promise(function(resolve, reject) {
          setTimeout(function() {
              console.log('time', time);
              resolve(time);
          }, time);
      });
    };
}

Promise.resolve([test(2000), test(400), test(1000), test(1), test(5000)])
       .mapSeries(function(asyncMethodPassed) {
                    return asyncMethodPassed();
}).then(function(results) {
    console.log('result', results);
});
like image 94
Ben Dadsetan Avatar answered Sep 23 '22 02:09

Ben Dadsetan