Timeline for How do I access previous promise results in a .then() chain?
Current License: CC BY-SA 3.0
35 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 17, 2019 at 19:48 | comment | added | Bergi |
@TeeJ Sorry, yes, promiseA is a function returning a promise and promiseA() is the actual promise. How these are fulfilled or rejected doesn't matter for the purposes of this question. If you're still unclear about resolve and reject being methods, please ask a new question where I answer in more detail.
|
|
| Oct 17, 2019 at 18:43 | comment | added | user8284384 | @Bergi I may have been reading this post and MDN for too long, but are PromiseA and PromiseB actually promises themselves? Are they not functions that return objects, which are promises themselves. And then those promises either result in the resolve or reject methods of the objects being executed? It's the final piece of the puzzle I am still confused about. | |
| Aug 28, 2019 at 0:07 | comment | added | David Spector | I see. Well, I've since learned that the new Async/Await syntax includes automatic binding of arguments, so all the arguments are available to all the asynchronous functions. Having worked so hard to discover how to use 'bind', which doesn't require using Promise.all(), I've decided to abandon Promises entirely. | |
| Aug 27, 2019 at 19:27 | comment | added | Bergi |
@DavidSpector It still is a chain, the callback being chained after promiseB() chained to promiseA(), imposing the order. By storing the individual promises in variables, using Promise.all does let us depend on values from multiple steps of the chain instead of just the last one.
|
|
| Aug 27, 2019 at 19:06 | comment | added | David Spector | I do not understand this example. If there is a chain of 'then' statements that require that values be propagated throughout the chain, I do not see how this solves the problem. A Promise that requires a previous value CANNOT be fired (created) until that value is present. Furthermore, Promise.all() simply waits for all promises in its list to finish: it does not impose an order. So I need each 'next' function to have access to all previous values and I don't see how your example does that. You should walk us through your example, because I do not believe it or understand it. | |
| Dec 4, 2018 at 21:54 | comment | added | Bergi | @reify No, you shouldn't do that, it would bring trouble with rejections. | |
| Dec 4, 2018 at 21:12 | comment | added | user10675354 |
Instead of Promise.all you can also use liftP2 et al: liftP2 = f => p => q => p.then(x => q.then(y => f(x, y))).
|
|
| Nov 23, 2018 at 2:48 | comment | added | tscizzle |
Any advice on naming promises when you store the result of Promise.all in a variable, so it's re-usable? Especially, if it is essentially just to make multiple values available to later code (as in this answer).
|
|
| Aug 30, 2017 at 8:19 | comment | added | Bergi |
@Roland Never said it was :-) This answer was written in the ES5 age where no promises were in the standard at all, and spread was super useful in this pattern. For more modern solutions see the accepted answer. However, I already updated the explicit-passthrough answer, and there's really no good reason not to update this one as well.
|
|
| Aug 30, 2017 at 8:14 | history | edited | Bergi | CC BY-SA 3.0 |
added 503 characters in body
|
| Jul 26, 2017 at 0:08 | comment | added | Bergi | @DragonKnight You might want to ask a separate question and include your code there. But in general, when you don't know how many elements you will have, use arrays. | |
| Jul 26, 2017 at 0:01 | comment | added | DragonKnight |
@Bergi my problem is I dont know how many promises are gonna be. there is a big chain of promises I have to handle and create elastic search query for each. what I did for now is when I return the response, I attach the data I need for the next query like return {response:response, data1:"d1",data2:"d2"} and access it in the then(function(data)) from response but it got very ugly.
|
|
| Jul 25, 2017 at 23:33 | comment | added | Bergi |
@DragonKnight What is in a loop? This questions and all the answers assume that you have a fixed number of promises whose results you want to access individually. If you're working with arrays of arbitrary size, use the result array that Promise.all generates.
|
|
| Jul 25, 2017 at 23:24 | comment | added | DragonKnight |
what if its in a for loop? how to handle arguments of .spread(function(resultA, resultB) ?
|
|
| May 23, 2017 at 12:34 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
| Jul 11, 2016 at 15:34 | comment | added | Rhayene | ahhh, now it makes sense that it didn't compile :D I just misunderstood your answer. I got it running with the spread operator. | |
| Jul 11, 2016 at 15:18 | comment | added | Bergi | @Rhayene: Yes, Bluebird, but also Q, when and other major promise libs implement it. It's becoming less important with destructuring in ES6. | |
| Feb 28, 2016 at 20:35 | comment | added | Bryan Downing |
The key for me was to actually return data from my Promise.all.then callback. Previously, I was returning the Promise.all callback, but I only had a console.log in my .then callback. This caused the data passed to any future promise callbacks to be undefined.
|
|
| Oct 28, 2015 at 15:00 | history | edited | Bergi | CC BY-SA 3.0 |
deleted 79 characters in body
|
| May 17, 2015 at 23:10 | vote | accept | Bergi | ||
| May 17, 2015 at 23:10 | |||||
| May 17, 2015 at 23:07 | vote | accept | Bergi | ||
| May 17, 2015 at 23:08 | |||||
| May 17, 2015 at 23:07 | vote | accept | Bergi | ||
| May 17, 2015 at 23:07 | |||||
| May 17, 2015 at 23:06 | vote | accept | Bergi | ||
| May 17, 2015 at 23:06 | |||||
| May 17, 2015 at 23:06 | vote | accept | Bergi | ||
| May 17, 2015 at 23:06 | |||||
| May 17, 2015 at 23:05 | vote | accept | Bergi | ||
| May 17, 2015 at 23:06 | |||||
| May 17, 2015 at 23:05 | vote | accept | Bergi | ||
| May 17, 2015 at 23:05 | |||||
| May 17, 2015 at 23:04 | vote | accept | Bergi | ||
| May 17, 2015 at 23:04 | |||||
| May 17, 2015 at 23:04 | vote | accept | Bergi | ||
| May 17, 2015 at 23:04 | |||||
| May 17, 2015 at 23:01 | vote | accept | Bergi | ||
| May 17, 2015 at 23:01 | |||||
| Apr 16, 2015 at 23:37 | comment | added | Bergi |
Yes, it's guaranteed that resultA comes from a and resultB comes from b, regardless of the order in which the results become available.
|
|
| Apr 16, 2015 at 20:41 | comment | added | scaryguy |
Oh yes, actually I meant promises :) It might be a lack of my understanding of promises, sorry though. So when you use .all or .join, promises return values in order, right?
|
|
| Apr 16, 2015 at 8:56 | comment | added | Bergi |
@scaryguy: There are no functions in the array, those are promises. promiseA and promiseB are the (promise-returning) functions here.
|
|
| Apr 16, 2015 at 5:15 | comment | added | scaryguy | Are the functions inside the array executed in order? | |
| Feb 2, 2015 at 17:22 | vote | accept | Bergi | ||
| Feb 6, 2015 at 8:44 | |||||
| Jan 31, 2015 at 10:44 | history | answered | Bergi | CC BY-SA 3.0 |