Yes, this was quite unintuitive the first time I saw something like that, but you can just loop over threads, promises, etc. and wait for each one to finish, since you're ultimately waiting for the one that runs the longest anyway, and even if it comes first, the others will then just resolve instantly.
I think what makes it doubly confusing is that a very common pattern for Promise.all is mapping over an array, so the callback to map takes a function resolving to a promise, even though what's directly being passed to Promise.all is the returned promises themselves.
But you could equally loop over the promises instead of the urls:
forawait(constdataofpromises){doSomething(data)}
Time taken: max latency of any one request.
Maybe this is just spelling out the obvious for some people but personally I still find it somewhat unintuitive, until you have that lightbulb moment 💡
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yes, this was quite unintuitive the first time I saw something like that, but you can just loop over threads, promises, etc. and wait for each one to finish, since you're ultimately waiting for the one that runs the longest anyway, and even if it comes first, the others will then just resolve instantly.
I think what makes it doubly confusing is that a very common pattern for
Promise.all
is mapping over an array, so the callback tomap
takes a function resolving to a promise, even though what's directly being passed toPromise.all
is the returned promises themselves.cb
is(url: String) => Promise<any>
, butpromises
isArray<Promise<any>>
, notArray<(url: String) => Promise<any>>
.To fetch and do something with each resolved object in series, you'd do this:
Time taken: total latency of all requests.
But you could equally loop over the promises instead of the urls:
Time taken: max latency of any one request.
Maybe this is just spelling out the obvious for some people but personally I still find it somewhat unintuitive, until you have that lightbulb moment 💡