TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

JavaScript Promise.all vs. Promise.allSettled

58 pointsby jonlucaabout 5 years ago

6 comments

rockostrichabout 5 years ago
&gt; I was reading the MDN docs on JavaScript promises and realized that the difference between Promise.all and Promise.allSettled wasn’t immediately obvious.<p>Really? I haven&#x27;t dealt with JavaScript in a while so I took a look at the MDN docs and it seems pretty obvious.<p>&gt; The Promise.all() method returns a single Promise that fulfills when all of the promises passed as an iterable have been fulfilled<p>and<p>&gt; The Promise.allSettled() method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.<p>`all` flattens all of the promises in a single promise containing an array of their results while `allSettled` flattens the promises into a single promise containing an array of objects that contain the resulting value as well as the status (which is either fulfilled or rejected).<p>The one catch that&#x27;s not immediately obvious is pointed out by the article which is that `all` will resolve on the first rejected promise. Except that&#x27;s also in the first paragraph of the doc:<p>&gt;It rejects with the reason of the first promise that rejects
评论 #23227210 未加载
评论 #23226112 未加载
billmalarkyabout 5 years ago
Nice to see Promise.race() in there. I&#x27;ve used it to emulate timeout logic on I&#x2F;O functionality. Useful when your JS is operating in an environment where there&#x27;s a timeout constraint you can&#x27;t control and you want your JS to gracefully handle timing out instead of the script just being killed.<p>Example use case is a background job running in JS via React Native. iOS limits these jobs to 30 seconds then kills the script. Promise.race() is one way to regain control of the thread and begin teardown prior to iOS killing the script, if for example the other promise is hanging past 30 seconds. Keep in mind this is not a true timeout, the other promise will continue to try to resolve until script is terminated.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;billmalarky&#x2F;react-native-queue&#x2F;blob&#x2F;5c45bf92a6c24275feb753cc95366e74c754a956&#x2F;Models&#x2F;Worker.js#L86-L125" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;billmalarky&#x2F;react-native-queue&#x2F;blob&#x2F;5c45b...</a>
jannesabout 5 years ago
The author seems to be a bit confused about the difference between &quot;throwing&quot; and &quot;rejecting&quot;.<p>&gt; Promise.allSettled can never throw<p>Of course not. And Promise.all neither. Promises don&#x27;t throw, they reject.<p>async&#x2F;await is syntactic sugar on top of that, which can make it look similar to throwing, but you will never actually &quot;throw&quot;.
评论 #23229328 未加载
darepublicalmost 5 years ago
I have never heard of all settled before and remember implementing that functionality myself some years back
londons_explorealmost 5 years ago
allSettled is quite new (it isn&#x27;t in the version of node packaged with LTS Ubuntu).<p>How does one implement the same with all?
评论 #23233345 未加载
flak48about 5 years ago
&gt; I was reading the MDN docs on JavaScript promises and realized that the difference between Promise.all and Promise.allSettled wasn’t immediately obvious.<p>Sorry, don&#x27;t agree with you there