2

I am using Promise.all() within a promise chain. Each promise within the Promise.all() returns a string.

The issue im having is that Promise.all() returns a Promise object to the next promise and I would like to continue the promise chain for each string.

Heres an example:

.... return Promise.all(plugins); }) .then(function(response) { console.log(response) .... 

The response looks like:

[ 'results from p1', 'results from p2' ] 

Is there any way to continue the promise chain for each of the results rather than continuing with a single object containing all results?

1

3 Answers 3

1

Promise.all expects an array of promises. So plugins is an array of promises and all the more: plugin is a Promise. So you can just chain your plugin Promise. This would thus become Promise.all(plugins.map(function(plugin){ return plugin.then(function(yourPluginString){ return 'example '+ yourPluginString; }) }))

Sign up to request clarification or add additional context in comments.

Comments

0

Promise.all(), by its design returns a single promise who's resolved value is an array of resolved values for all the promises you passed it. That's what it does. If that isn't what you want, then perhaps you are using the wrong tool. You can process the individual results in a number of ways:

First, you can just loop over the array of returned results and do whatever you want to with them for further processing.

Promise.all(plugins).then(function(results) { return results.map(function(item) { // can return either a value or another promise here return .... }); }).then(function(processedResults) { // process final results array here }) 

Second, you could attach a .then() handler to each individual promise BEFORE you pass it to Promise.all().

// return new array of promises that has done further processing // before passing to Promise.all() var array = plugins.map(function(p) { return p.then(function(result) { // do further processing on the individual result here // return something (could even be another promise) return xxx; }); }) Promise.all(array).then(function(results) { // process final results array here }); 

Or, third if you don't really care when all the results are done and you just want to process each one individually, then don't use Promise.all() at all. Just attach a .then() handler to each individual promise and process each result as it happens.

1 Comment

Very helpful indeed. Thanks for the detailed options and examples.
0

You can use a tool like https://github.com/Raising/PromiseChain

and implement what you say as

//sc = internalScope var sc = {}; new PromiseChain(sc) .continueAll([plugin1,plugin2,plugin3],function(sc,plugin){ return plugin(); // I asume this return a promise },"pluginsResults") .continueAll(sc.pluginsResults,function(sc,pluginResult){ return handlePluginResults(pluginResult); },"operationsResults") .end(); 

I didnt test the code if you have any problem PM me

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.