Bluebird mapSeries

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

The iterator will not be called for the element until its previous element, and the promise returned by the iterator for this element will be 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); }); 

I expect console.log inside the test function to display: 2000, 1000, 500, 3000 in that order. I expect that because, as the docs say, each element only goes after the previous one is allowed. Instead, I get 500, 1000, 2000, 3000, which reflects the fact that all functions are called instantly. Moreover, the results show the results in the order in which they were called, although this does not matter at this point.

Do I really understand something?

+5
source share
1 answer

Your test calls will run before Promise.mapSeries . Also, mapSeries usually runs on a promise instance. Maybe the following example helps to understand? Note that the test (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); }); 
+6
source

Source: https://habr.com/ru/post/1264995/


All Articles