How does an array of promises work exactly with reduce?












6















I was reading this article HERE, which speaks about how can you use reduce with promises and in the end the following snippet is shown:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


So without changing much of the code , i made the following demo:



const tasks = [ fetch('https://jsonplaceholder.typicode.com/todos/1') , 
fetch('https://jsonplaceholder.typicode.com/todos/2') ,
fetch('https://jsonplaceholder.typicode.com/todos/3') ];


tasks.reduce((promiseChain, currentTask) => {

console.log(promiseChain);

return promiseChain.then(chainResults => {
return currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
});
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
console.log(arrayOfResults);
});


But i still don't understand , since reduce is simplistically just a forEach loop and inside reduce we are relying on a promise being returned, what is the guareenty that the reduce function will not just loop though all the elements in the array (in this case an array of promises) , without the then() firing ?










share|improve this question




















  • 1





    Its promises all the way down, it is just looping through the reduce elements and the code is structured in such a way that you are creating a large chain of .then operations. I can see reasons to do something like this (you need intermediate values to make the next call), but in your example case Promise.all(tasks).then() would likely be the better solution.

    – D Lowther
    Nov 13 '18 at 12:07






  • 1





    An array of promises doesn't work with reduce at all, you'd just use Promise.all. You are talking about an array of tasks (functions that return promises) however.

    – Bergi
    Nov 13 '18 at 12:43






  • 2





    …and indeed, the code you posted here does not work sequentially, it does the three fetch requests immediately.

    – Bergi
    Nov 13 '18 at 12:47











  • "reduce is simplistically just a forEach loop" - better to look at it the other way round: forEach is just a reduce loop but ignoring return values.

    – Bergi
    Nov 13 '18 at 20:20






  • 1





    You might want to have a look at stackoverflow.com/a/30823708/1048572

    – Bergi
    Nov 13 '18 at 20:22
















6















I was reading this article HERE, which speaks about how can you use reduce with promises and in the end the following snippet is shown:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


So without changing much of the code , i made the following demo:



const tasks = [ fetch('https://jsonplaceholder.typicode.com/todos/1') , 
fetch('https://jsonplaceholder.typicode.com/todos/2') ,
fetch('https://jsonplaceholder.typicode.com/todos/3') ];


tasks.reduce((promiseChain, currentTask) => {

console.log(promiseChain);

return promiseChain.then(chainResults => {
return currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
});
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
console.log(arrayOfResults);
});


But i still don't understand , since reduce is simplistically just a forEach loop and inside reduce we are relying on a promise being returned, what is the guareenty that the reduce function will not just loop though all the elements in the array (in this case an array of promises) , without the then() firing ?










share|improve this question




















  • 1





    Its promises all the way down, it is just looping through the reduce elements and the code is structured in such a way that you are creating a large chain of .then operations. I can see reasons to do something like this (you need intermediate values to make the next call), but in your example case Promise.all(tasks).then() would likely be the better solution.

    – D Lowther
    Nov 13 '18 at 12:07






  • 1





    An array of promises doesn't work with reduce at all, you'd just use Promise.all. You are talking about an array of tasks (functions that return promises) however.

    – Bergi
    Nov 13 '18 at 12:43






  • 2





    …and indeed, the code you posted here does not work sequentially, it does the three fetch requests immediately.

    – Bergi
    Nov 13 '18 at 12:47











  • "reduce is simplistically just a forEach loop" - better to look at it the other way round: forEach is just a reduce loop but ignoring return values.

    – Bergi
    Nov 13 '18 at 20:20






  • 1





    You might want to have a look at stackoverflow.com/a/30823708/1048572

    – Bergi
    Nov 13 '18 at 20:22














6












6








6


1






I was reading this article HERE, which speaks about how can you use reduce with promises and in the end the following snippet is shown:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


So without changing much of the code , i made the following demo:



const tasks = [ fetch('https://jsonplaceholder.typicode.com/todos/1') , 
fetch('https://jsonplaceholder.typicode.com/todos/2') ,
fetch('https://jsonplaceholder.typicode.com/todos/3') ];


tasks.reduce((promiseChain, currentTask) => {

console.log(promiseChain);

return promiseChain.then(chainResults => {
return currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
});
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
console.log(arrayOfResults);
});


But i still don't understand , since reduce is simplistically just a forEach loop and inside reduce we are relying on a promise being returned, what is the guareenty that the reduce function will not just loop though all the elements in the array (in this case an array of promises) , without the then() firing ?










share|improve this question
















I was reading this article HERE, which speaks about how can you use reduce with promises and in the end the following snippet is shown:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


So without changing much of the code , i made the following demo:



const tasks = [ fetch('https://jsonplaceholder.typicode.com/todos/1') , 
fetch('https://jsonplaceholder.typicode.com/todos/2') ,
fetch('https://jsonplaceholder.typicode.com/todos/3') ];


tasks.reduce((promiseChain, currentTask) => {

console.log(promiseChain);

return promiseChain.then(chainResults => {
return currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
});
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
console.log(arrayOfResults);
});


But i still don't understand , since reduce is simplistically just a forEach loop and inside reduce we are relying on a promise being returned, what is the guareenty that the reduce function will not just loop though all the elements in the array (in this case an array of promises) , without the then() firing ?







javascript es6-promise






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 12:08







Alexander Solonik

















asked Nov 13 '18 at 11:16









Alexander SolonikAlexander Solonik

3,41063386




3,41063386








  • 1





    Its promises all the way down, it is just looping through the reduce elements and the code is structured in such a way that you are creating a large chain of .then operations. I can see reasons to do something like this (you need intermediate values to make the next call), but in your example case Promise.all(tasks).then() would likely be the better solution.

    – D Lowther
    Nov 13 '18 at 12:07






  • 1





    An array of promises doesn't work with reduce at all, you'd just use Promise.all. You are talking about an array of tasks (functions that return promises) however.

    – Bergi
    Nov 13 '18 at 12:43






  • 2





    …and indeed, the code you posted here does not work sequentially, it does the three fetch requests immediately.

    – Bergi
    Nov 13 '18 at 12:47











  • "reduce is simplistically just a forEach loop" - better to look at it the other way round: forEach is just a reduce loop but ignoring return values.

    – Bergi
    Nov 13 '18 at 20:20






  • 1





    You might want to have a look at stackoverflow.com/a/30823708/1048572

    – Bergi
    Nov 13 '18 at 20:22














  • 1





    Its promises all the way down, it is just looping through the reduce elements and the code is structured in such a way that you are creating a large chain of .then operations. I can see reasons to do something like this (you need intermediate values to make the next call), but in your example case Promise.all(tasks).then() would likely be the better solution.

    – D Lowther
    Nov 13 '18 at 12:07






  • 1





    An array of promises doesn't work with reduce at all, you'd just use Promise.all. You are talking about an array of tasks (functions that return promises) however.

    – Bergi
    Nov 13 '18 at 12:43






  • 2





    …and indeed, the code you posted here does not work sequentially, it does the three fetch requests immediately.

    – Bergi
    Nov 13 '18 at 12:47











  • "reduce is simplistically just a forEach loop" - better to look at it the other way round: forEach is just a reduce loop but ignoring return values.

    – Bergi
    Nov 13 '18 at 20:20






  • 1





    You might want to have a look at stackoverflow.com/a/30823708/1048572

    – Bergi
    Nov 13 '18 at 20:22








1




1





Its promises all the way down, it is just looping through the reduce elements and the code is structured in such a way that you are creating a large chain of .then operations. I can see reasons to do something like this (you need intermediate values to make the next call), but in your example case Promise.all(tasks).then() would likely be the better solution.

– D Lowther
Nov 13 '18 at 12:07





Its promises all the way down, it is just looping through the reduce elements and the code is structured in such a way that you are creating a large chain of .then operations. I can see reasons to do something like this (you need intermediate values to make the next call), but in your example case Promise.all(tasks).then() would likely be the better solution.

– D Lowther
Nov 13 '18 at 12:07




1




1





An array of promises doesn't work with reduce at all, you'd just use Promise.all. You are talking about an array of tasks (functions that return promises) however.

– Bergi
Nov 13 '18 at 12:43





An array of promises doesn't work with reduce at all, you'd just use Promise.all. You are talking about an array of tasks (functions that return promises) however.

– Bergi
Nov 13 '18 at 12:43




2




2





…and indeed, the code you posted here does not work sequentially, it does the three fetch requests immediately.

– Bergi
Nov 13 '18 at 12:47





…and indeed, the code you posted here does not work sequentially, it does the three fetch requests immediately.

– Bergi
Nov 13 '18 at 12:47













"reduce is simplistically just a forEach loop" - better to look at it the other way round: forEach is just a reduce loop but ignoring return values.

– Bergi
Nov 13 '18 at 20:20





"reduce is simplistically just a forEach loop" - better to look at it the other way round: forEach is just a reduce loop but ignoring return values.

– Bergi
Nov 13 '18 at 20:20




1




1





You might want to have a look at stackoverflow.com/a/30823708/1048572

– Bergi
Nov 13 '18 at 20:22





You might want to have a look at stackoverflow.com/a/30823708/1048572

– Bergi
Nov 13 '18 at 20:22












1 Answer
1






active

oldest

votes


















1














Ok, back from reading the article with a more complete answer. This tactic is for async tasks that rely on one another, but aren't always the same. If they were a fixed structure you would do (from the example):



return task1.then(result1 =>
task2.then(result2 =>
task3.then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


Think of this in terms of doing maybe database work.



return dbOrm.task1.create()
.then(newRecord =>
// task 2 requires the id from the new record to operate
dbOrm.task2.create({task1_id: newRecord.id})
.then(result2 =>
// some other async that relies on result2
task3(result2).then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


This is a fixed set of dependencies, they rely on each other to operate and you need the results of all of them to continue.



The example you linked is meant for that kind of serial execution but in a situation with non-fixed dependencies. Reduce is being used to synchronously constructing a chain of async actions that can then resolve on their own, and since the reduce function is returning a resolved promise you can chain another function off the end of it to handle the completed chain.



Reduce is more than a forEach loop even though they both use iteration to move over an array. Reduce also passes the returned result of the previous iteration to the next iteration. You are 'Reducing' your group of tasks down to one completed task; forEach doesn't act to change the array it is operating on (and Map, another iterating array function, returns a modified new array from the old).



so using the example code:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


You would get something like this given 2 iterataions:



// 1
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])

// 2
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])

// the then after the reduce
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])
.then(arrayOfResults => //do Something)
.catch(//because you should handle errors)





share|improve this answer



















  • 1





    please give me time to go though your answer and accept . thanks for the help +1 .

    – Alexander Solonik
    Nov 14 '18 at 19:15











  • Brilliant explanation ! Thanks a ton , absolutely brilliant ! Mind blowing ... FANTASTIC !

    – Alexander Solonik
    Nov 18 '18 at 22:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279833%2fhow-does-an-array-of-promises-work-exactly-with-reduce%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Ok, back from reading the article with a more complete answer. This tactic is for async tasks that rely on one another, but aren't always the same. If they were a fixed structure you would do (from the example):



return task1.then(result1 =>
task2.then(result2 =>
task3.then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


Think of this in terms of doing maybe database work.



return dbOrm.task1.create()
.then(newRecord =>
// task 2 requires the id from the new record to operate
dbOrm.task2.create({task1_id: newRecord.id})
.then(result2 =>
// some other async that relies on result2
task3(result2).then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


This is a fixed set of dependencies, they rely on each other to operate and you need the results of all of them to continue.



The example you linked is meant for that kind of serial execution but in a situation with non-fixed dependencies. Reduce is being used to synchronously constructing a chain of async actions that can then resolve on their own, and since the reduce function is returning a resolved promise you can chain another function off the end of it to handle the completed chain.



Reduce is more than a forEach loop even though they both use iteration to move over an array. Reduce also passes the returned result of the previous iteration to the next iteration. You are 'Reducing' your group of tasks down to one completed task; forEach doesn't act to change the array it is operating on (and Map, another iterating array function, returns a modified new array from the old).



so using the example code:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


You would get something like this given 2 iterataions:



// 1
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])

// 2
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])

// the then after the reduce
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])
.then(arrayOfResults => //do Something)
.catch(//because you should handle errors)





share|improve this answer



















  • 1





    please give me time to go though your answer and accept . thanks for the help +1 .

    – Alexander Solonik
    Nov 14 '18 at 19:15











  • Brilliant explanation ! Thanks a ton , absolutely brilliant ! Mind blowing ... FANTASTIC !

    – Alexander Solonik
    Nov 18 '18 at 22:41
















1














Ok, back from reading the article with a more complete answer. This tactic is for async tasks that rely on one another, but aren't always the same. If they were a fixed structure you would do (from the example):



return task1.then(result1 =>
task2.then(result2 =>
task3.then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


Think of this in terms of doing maybe database work.



return dbOrm.task1.create()
.then(newRecord =>
// task 2 requires the id from the new record to operate
dbOrm.task2.create({task1_id: newRecord.id})
.then(result2 =>
// some other async that relies on result2
task3(result2).then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


This is a fixed set of dependencies, they rely on each other to operate and you need the results of all of them to continue.



The example you linked is meant for that kind of serial execution but in a situation with non-fixed dependencies. Reduce is being used to synchronously constructing a chain of async actions that can then resolve on their own, and since the reduce function is returning a resolved promise you can chain another function off the end of it to handle the completed chain.



Reduce is more than a forEach loop even though they both use iteration to move over an array. Reduce also passes the returned result of the previous iteration to the next iteration. You are 'Reducing' your group of tasks down to one completed task; forEach doesn't act to change the array it is operating on (and Map, another iterating array function, returns a modified new array from the old).



so using the example code:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


You would get something like this given 2 iterataions:



// 1
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])

// 2
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])

// the then after the reduce
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])
.then(arrayOfResults => //do Something)
.catch(//because you should handle errors)





share|improve this answer



















  • 1





    please give me time to go though your answer and accept . thanks for the help +1 .

    – Alexander Solonik
    Nov 14 '18 at 19:15











  • Brilliant explanation ! Thanks a ton , absolutely brilliant ! Mind blowing ... FANTASTIC !

    – Alexander Solonik
    Nov 18 '18 at 22:41














1












1








1







Ok, back from reading the article with a more complete answer. This tactic is for async tasks that rely on one another, but aren't always the same. If they were a fixed structure you would do (from the example):



return task1.then(result1 =>
task2.then(result2 =>
task3.then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


Think of this in terms of doing maybe database work.



return dbOrm.task1.create()
.then(newRecord =>
// task 2 requires the id from the new record to operate
dbOrm.task2.create({task1_id: newRecord.id})
.then(result2 =>
// some other async that relies on result2
task3(result2).then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


This is a fixed set of dependencies, they rely on each other to operate and you need the results of all of them to continue.



The example you linked is meant for that kind of serial execution but in a situation with non-fixed dependencies. Reduce is being used to synchronously constructing a chain of async actions that can then resolve on their own, and since the reduce function is returning a resolved promise you can chain another function off the end of it to handle the completed chain.



Reduce is more than a forEach loop even though they both use iteration to move over an array. Reduce also passes the returned result of the previous iteration to the next iteration. You are 'Reducing' your group of tasks down to one completed task; forEach doesn't act to change the array it is operating on (and Map, another iterating array function, returns a modified new array from the old).



so using the example code:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


You would get something like this given 2 iterataions:



// 1
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])

// 2
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])

// the then after the reduce
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])
.then(arrayOfResults => //do Something)
.catch(//because you should handle errors)





share|improve this answer













Ok, back from reading the article with a more complete answer. This tactic is for async tasks that rely on one another, but aren't always the same. If they were a fixed structure you would do (from the example):



return task1.then(result1 =>
task2.then(result2 =>
task3.then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


Think of this in terms of doing maybe database work.



return dbOrm.task1.create()
.then(newRecord =>
// task 2 requires the id from the new record to operate
dbOrm.task2.create({task1_id: newRecord.id})
.then(result2 =>
// some other async that relies on result2
task3(result2).then(result3 =>
[ result1, result2, result3 ]
)
)
).then(arrayOfResults => {
// Do something with all results
});


This is a fixed set of dependencies, they rely on each other to operate and you need the results of all of them to continue.



The example you linked is meant for that kind of serial execution but in a situation with non-fixed dependencies. Reduce is being used to synchronously constructing a chain of async actions that can then resolve on their own, and since the reduce function is returning a resolved promise you can chain another function off the end of it to handle the completed chain.



Reduce is more than a forEach loop even though they both use iteration to move over an array. Reduce also passes the returned result of the previous iteration to the next iteration. You are 'Reducing' your group of tasks down to one completed task; forEach doesn't act to change the array it is operating on (and Map, another iterating array function, returns a modified new array from the old).



so using the example code:



const tasks = getTaskArray();
return tasks.reduce((promiseChain, currentTask) => {
return promiseChain.then(chainResults =>
currentTask.then(currentResult =>
[ ...chainResults, currentResult ]
)
);
}, Promise.resolve()).then(arrayOfResults => {
// Do something with all results
});


You would get something like this given 2 iterataions:



// 1
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])

// 2
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])

// the then after the reduce
Promise.resolve()
.then(chainResults => task1().then(task1Result => ([ ...chainResults, task1Result])
.then(chainResults => task2().then(task2Result => ([ ...chainResults, task2Result])
.then(arrayOfResults => //do Something)
.catch(//because you should handle errors)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 12:39









D LowtherD Lowther

1,3181414




1,3181414








  • 1





    please give me time to go though your answer and accept . thanks for the help +1 .

    – Alexander Solonik
    Nov 14 '18 at 19:15











  • Brilliant explanation ! Thanks a ton , absolutely brilliant ! Mind blowing ... FANTASTIC !

    – Alexander Solonik
    Nov 18 '18 at 22:41














  • 1





    please give me time to go though your answer and accept . thanks for the help +1 .

    – Alexander Solonik
    Nov 14 '18 at 19:15











  • Brilliant explanation ! Thanks a ton , absolutely brilliant ! Mind blowing ... FANTASTIC !

    – Alexander Solonik
    Nov 18 '18 at 22:41








1




1





please give me time to go though your answer and accept . thanks for the help +1 .

– Alexander Solonik
Nov 14 '18 at 19:15





please give me time to go though your answer and accept . thanks for the help +1 .

– Alexander Solonik
Nov 14 '18 at 19:15













Brilliant explanation ! Thanks a ton , absolutely brilliant ! Mind blowing ... FANTASTIC !

– Alexander Solonik
Nov 18 '18 at 22:41





Brilliant explanation ! Thanks a ton , absolutely brilliant ! Mind blowing ... FANTASTIC !

– Alexander Solonik
Nov 18 '18 at 22:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279833%2fhow-does-an-array-of-promises-work-exactly-with-reduce%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ