Promise.all is good for doing multiple queries with no side effects where you need all to succeed before the next step. You could do all settled and add retry logic but I’d rather have it in the work function or have the whole thing retried instead
@awmy3109 Жыл бұрын
Exactly. Devs just worry about BS these days 😂
@kartashuvit4971 Жыл бұрын
wouldn't it be better to start a transaction instead
@tuomaskoivistoinen6476 Жыл бұрын
@@kartashuvit4971 that question is a bit orthogonal to how you handle promises. If a transaction would prevent significant bugs then yes, otherwise you get better performance committing a single statement at a time.
@nikensss Жыл бұрын
sounds to me this video is needed because people don't read the documentation thoroughly enough
@developersteve1658 Жыл бұрын
@awmy3109 I think it's that they less worry about bullshit and more and more that the majority use case doesn't require a surgical solution like this. If you have some really slow process that runs many times (let's say some api call that costs for time used), it would make sense to try and cache the successful results and retry the failures, because it could save a lot of time and money in the long-term. For loading 1kb of json from your own api for a component to render "hello [user]", yeah that can be retried wholesale.
@samuelgunter Жыл бұрын
if you use all instead of allSettled, this video would be over at 0:10 because that's when the first Exception is thrown
@ropoxdev Жыл бұрын
Haha
@Notoriousjunior374 Жыл бұрын
You should have a function that catches that error, do whatever you want with it, null, undefined or whatever. Then it wouldn’t be a problem?
@avidrucker Жыл бұрын
Also interested to know
@Alec.Vision Жыл бұрын
4:09 Bruh, that's what Exclude is for. The best thing about Exclude is that it will exclude anything that EXTENDS the second type param... so you can exclude a whole object type from a discriminated union by specifying only the discriminating property. You don't have to know the types, just the difference.
@Glyra12 Жыл бұрын
didnt knew about this nice one
@kevinmitchell6141 Жыл бұрын
Could you give an example of how you use this?
@LLF1234 Жыл бұрын
@@kevinmitchell6141 If he doesn't, ChatGPT probably does
@pellyryu23449 ай бұрын
nice tip thanks
@robgioeli Жыл бұрын
I’m glad you got this all settled, thanks Theo 😉
@EvanBoldt Жыл бұрын
Seems like another example of how returning errors results in better error handling than throwing errors.
@alexandrucaraus233 Жыл бұрын
Throwing is more OOP, returning is more functional approach. TS supports both, so it all depends how and when you use it.
@bryanleebmy Жыл бұрын
@@alexandrucaraus233 Nothing about throwing or returning is inherently OOP or functional. Throwing is just the model most commonly used by Java, which happens to be the most widely taught OOP language. In an alternate universe, returning errors or result types could have been the standard way of doing things in Java. OOP != throwing.
@kartashuvit4971 Жыл бұрын
That's why I'm slowly falling in love with Go
@alexandrucaraus233 Жыл бұрын
@@bryanleebmy yes, java, c#, c++ and all other other OOP languages use more the throwing approach, and all other pure functional prefer returning values. So blue!=green, like I said :D.
@felipeandrade66298 ай бұрын
Said the Go developer, lol
@Xe054 Жыл бұрын
I love this type of content where you share new discoveries about a language. Please make more videos like this.
@alanscodelog Жыл бұрын
I usually use .all and catch in the entry promises and handle any errors and cleanup there and return or push the errors to an array (I find this nicer and more flexible than allSettled). The final .all then only serves to send off /format the errors if needed, or to take the final decision on what to do, not handle the errors themselves.
@gosnooky Жыл бұрын
To be fair the "^" operator being XOR and not exponent is common to most languages? As for the topic at hand, I'm guilty. I use Promise.all all the time.
@CottidaeSEA Жыл бұрын
I believe it's the standard. Most languages have something like Math.pow() or pow() instead of **.
@theoneandonlymeshe9174 Жыл бұрын
“I hate JavaScript” ~ Theo, Aug 2023 1:34
@elevyg Жыл бұрын
It is easier to check if the item of the result has value as key, then you know that the status is fulfilled
@Bluesourboy Жыл бұрын
I have this issue in some production apps that were not written by me that this will fix. Theo you are a godsend!
@AlexSpieslechner Жыл бұрын
summoning ts wizard matt to make a video on this type
@williamdrum9899 Жыл бұрын
"Don't make promises you can't keep" seems to be the moral here
@schlopping Жыл бұрын
honestly this is the best argument I've seen for a Result type, and it wasn't even intended.
@MKorostoff Жыл бұрын
Great video, but for the most part, I want Promise.all to reject if a single underlying promise rejects. For instance, I was recently writing some code that split PDFs into chunks for search indexing. If one single page cannot be chunked, I don't want to store incomplete data, I want to show a message like "example.pdf failed on page X." Promise.all is essentially like a database transaction in that way, and very convenient for that purpose (though I agree, allSettled has many great use cases).
@xinaesthetic Жыл бұрын
Do you have a strategy for cancelling other tasks when one fails?
@MKorostoff Жыл бұрын
@@xinaesthetic like if you had promises foo and bar, and you want to init them at the same time, then cancel bar if foo fails? Personally, I wouldn't, I'd just let bar finish.
@xinaesthetic Жыл бұрын
@@MKorostoff fair enough. No point coding it when you don't need it. In a situation where it did matter I suppose I'd probably have some shared reference to a bit of state that flags if the operation should be cancelled, but not sure what particular patterns there are that might be good to know.
Жыл бұрын
Basically if your requirement is to process a bunch of steps sequentially, which means one after the other, please forget Promise.all because it process everything CONCURRENTLY.
@dtinth Жыл бұрын
4:54 - When seeing `.filter` followed by `.map`, using `.flatMap` will usually result in a cleaner code: `results.flatMap(r => r.status === 'fulfilled' ? [r.value] : [])` When you use flatMap, filtering and mapping is done in a single step, so there is no loss of type information between these 2 steps.
@NickServ Жыл бұрын
True, but transduce also fixes this problem without introducing intermediate arrays, which will use more memory and power to merge.
@magne6049 Жыл бұрын
@@NickServ which transducers library do you recommend for JS?
@karamuto1565 Жыл бұрын
I am more into observables by now. There I have multiple choices to handle multi asyncs and it feels easier.
@TJKlimoski10 ай бұрын
I actually just used promise.all in a project that to me was appropriate. Essentially I'm running two async functions in parallel that return an array of values. Once I get back both arrays I compare the two arrays and keep the values that are shared between the two. If one promise rejects (which only happens because they found no values to return in their array) I don't need to know what the returned values in the other async function will be because I know there will be no matching values for me to pull. So the fact that it jumps to the catch statement, and I don't get the values for the other promises, all work out.
@guilhermegoncalves67439 ай бұрын
Promise.all is good when youre using puppeteer. Great video!
@vncntjms Жыл бұрын
I prefer to just retry everything. That's the reason why I put them all together in one promise in the first place.
@Tszyu01 Жыл бұрын
I usually use a library like p-all or p-limit to also ensure promise all runs with a specified amount of concurrency.
@Voidstroyer Жыл бұрын
Nothing dangerous about promise.all if you know what it does (You might argue with me because I said "if you know what it does" but I say that because the function itself is not dangerous, it's the misuse of the function that is dangerous). Promise.all requires all functions to resolve otherwise nothing is returned (which does resolve in discarded work being done). Promise.allSettled is a good alternative though but if the requirement is that all promises MUST resolve (and you don't have a retry mechanism which could potentially also cause other complexities because endless retries would cause your program to hang) then promise.all is the preffered choice.
@daniellchukwu9 ай бұрын
I can't reject this one. God bless
@pankuka23 Жыл бұрын
If I need all results to succeed, and I don't want to keep it running on failed as .all does, I just use an async for loop - works wonders ;)
@sobanya_228 Жыл бұрын
I use it with something that's not an array. When you request multiple things, but definitely need all of it. An array api in that situation is kinda bad, I even defined a dictionary version of Promise.all, where you give names to each entry.
@Holdino_ Жыл бұрын
I've been awaiting this video
@luc122c Жыл бұрын
I find using a promise pool is a good balance. They usually return results and errors as separate objects, and have other features such as setting concurrency
@RealDrDoom Жыл бұрын
Maybe this is an anti-pattern but what if you catch inside the promises? If there's an error return null, otherwise return the result. That way the promises always resolve and we can filter out results after
@festusyuma1901 Жыл бұрын
If you use a for loop instead of a filter, you'll get the correct typing
@umuden Жыл бұрын
Yes, the error behavior isn't too inferable. Once, instead of reading the docs, I wrote my own makeConsecutively function. Which was fine.
@trappedcat3615 Жыл бұрын
I don't get it. I'm still awaiting the catch. Let me know when this is all settled.
@alexandrucaraus233 Жыл бұрын
All is settled, nothing to catch, go home :D.
@ColinRichardson11 ай бұрын
New Title = I used the wrong method for what I really want.. but what I want is not what 95% of everyone else wants.
@ziadx36 ай бұрын
thank god typescript fixed this filter issue
@Aetherius218 Жыл бұрын
You could also implement error catching in your asynchronous function (e.g., WORK) and have it return the potentially caught error. Then you can filter the returned array by instanceof Error
@Notoriousjunior374 Жыл бұрын
This, I don’t know what’s wrong with the limited amount of creativity in devs these days.
@Alec.Vision Жыл бұрын
Lol, Promise.all(promises.map(p=>p.catch(e=>e))) Can I haz utoob job plz?
@ellisgl Жыл бұрын
I just keep thinking of the song "Promises" by Fugazi...
@xaviersavinon5851 Жыл бұрын
Thanks, I didn't know that allSettled existed. Before this video, I only used Promise.All like this: The function WORK would be an async func with a try/catch so, if the promise got resolved would returned the value if not returned a null. After that filter the array of nulls.
@bozhidarmanev6772 Жыл бұрын
Theo is close to embracing a properly typed language!
@johnmcparland3277 Жыл бұрын
"JS Dev discovers Result and/or Option" is my favorite genre of programming video
@valenciawalker64989 ай бұрын
Thank you very helpful.
@pelle4971 Жыл бұрын
How might our understanding and application of asynchronous functions change if we were to prioritize clarity and predictability of outcomes over simplicity or convention in our coding practices?
@SkyaTura Жыл бұрын
The type issue couldn't be solved by using Exclude?
@3ventic Жыл бұрын
Isn't the .filter and .value issue one of those things that ts-reset fixes?
@sn0wb0ardfreak72 Жыл бұрын
promise.All() seems more reasonable in a serverless backend env during boot / initialization (eg. AWS SSM)
@jewbarrymore_9 ай бұрын
It was super hard not to notice and resist commenting, but why is waitFor declared as async? Seems kinda extra to wrap the return value when you're already returning a promise? Obviously, js handles it behind the scenes gracefully, but still, there's no await used inside.
@d3vilm4ster Жыл бұрын
Well not all is Settled in JS async yet
@dev1333 Жыл бұрын
oh no... dont do this to me theo
@Nackenschelle Жыл бұрын
splendid moustache, theo 🤝🏼
@joshrogan3577 Жыл бұрын
Is it fine to do promise.all with tanstack query mutateAsync? Say I need to make 5 separate independent mutations on button click.
@RTWeaver Жыл бұрын
I use it when building my test environment on the backend. I don't really care if the server or database failed to standup, if either of them fails then its pointless to run the test suite. Same with performing a bunch of bulkCreates with sequelize. If one of the bulkCreates fails then the database is corrupted anyway and I'm going to have to find out whats wrong and fix it regardless so it doesn't matter if the promises still run in the background or send back results.
@Fernando-ry5qt Жыл бұрын
The bulk case if you work with a bunch of operations of that nature you might use a Sagas pattern and revert/delete the corrupted data and retry it in some cases, but yeah I get what you mean
@benkogan1579 Жыл бұрын
Another thing is that the Promise concurrency functions are only useful for IO / network related tasks, since the requests are in flight in parallel. However, if you just have a very expensive task computationally, splitting it up and Promise.alling it wont speed it up at all, since JS is single-threaded and it will only ever work on one task at a time. If, however, you used multiple threads operating on multiple cores, then it would make sense.
@aredrih6723 Жыл бұрын
You kind always turn computation heavy task into io with webWorker. It just requires a lot more setup (create worker, send relevant data, await response from worker)
@NickServ Жыл бұрын
This is a bit of an oversimplification, since while it can result in performance issues compared to threads, single-thread concurrency with delaying Promises until next tick can still improve performance in many situations.
@benkogan1579 Жыл бұрын
@@NickServ for example?
@aredrih6723 Жыл бұрын
@@NickServ do you mean performance (how fast your code run end to end) or reactivity (how long your code take to react to an event)? If reactivity, I agree. If performance, if you have say 1000 lines of code to run to complete a function, splitting it into 10 group of 100 lines won't make things end faster.
@NickServ Жыл бұрын
@@aredrih6723 Both. Promise concurrency still helps with e2e wall time, especially when getting results early with all/race/any.
@kylegilbert5089 Жыл бұрын
Wow, this is actually an issue in one of my apps and I wasn't sure what the problem was. Ty Theo.
@paulomattos6753 Жыл бұрын
Promise ALL is the best for select queries
@lengors73275 ай бұрын
The filter issue was solved with TS5.5 🎉
@errormaker1 Жыл бұрын
That is unsettling
@snatvb Жыл бұрын
promises is sucks because they are not lazy and not cancellable and this might leads to unexpected results, data-race in async useEffect in react for example
@DryBones111 Жыл бұрын
The typing on the settled promise is so gross 😭
@klausburgersten Жыл бұрын
wdym hard to work with async stuff in js? have you tried to do this in python?
@alexaka1 Жыл бұрын
This behavior makes me like RxJS.
@appuser Жыл бұрын
I haven't finished the video yet, but I'm going to guess most of the time taking a less FP approach and using "for...in" within an already async function is the recommendation coming up, rather than managing the nesty confusion of Promise.all
@appuser Жыл бұрын
I was so wrong and learned something useful.
@owenwexler7214 Жыл бұрын
Don’t worry, the Promise puns will never not be funny… although I’m sure some people will… await the day when this isn’t true.
@mattmmilli828710 ай бұрын
Use await in a for-in loop(you can indeed!) to do them sequentially and promise.all for all at once
@munnakumarbarnwal1694 Жыл бұрын
Great video 😍, Thanks
@kartashuvit4971 Жыл бұрын
The type issue is actually easy to solve npm uninstall -g typescript
@willbdev Жыл бұрын
Thank you
@john.dough. Жыл бұрын
1:06 -> the carrot operator `^` is bitwise OR in JavaScript. You probably wanted Math.pow(). nvm, you caught it.
@alexanderpedenko6669 Жыл бұрын
Who is this video for, who immediately started writing on React without learning JS basics?
@Kay8B Жыл бұрын
I only used promise.all when theres a dependancy chain, a relies on b relies on c. If A failed we dont want to continue. Otherwise I agree settled for everything else.
@t3dotgg Жыл бұрын
Wouldn’t you need to await in sequence if they are dependent?
@Kay8B Жыл бұрын
@@t3dotgg Right my example wasnt clear, they would rely on eachother or another dependant requires all 3. Not sequentially. not in fact a proper "chain" more like a web
@MrJester831 Жыл бұрын
Futures > Promises because polling can be halted
@code.smiles Жыл бұрын
Sorry for the *chain* of puns...
@spicybaguette7706 Жыл бұрын
JS error handling in general sucks IMHO, especially with promises. Even with typescript you have no way of knowing if a function could throw and what it would throw, I hate it
@_worksonmymachine Жыл бұрын
I'm just here for the puns.
@stevenhe3462 Жыл бұрын
Because JavaScript Promises are fucking eager.
@danypell2517 Жыл бұрын
another gem
@possumkeys Жыл бұрын
Use case: Start up app requirements being fetched in calls. If any fails, we need to start all over again.
@t3dotgg Жыл бұрын
Wouldn't it be nice to know WHICH failed?
@garretmh Жыл бұрын
@@t3dotgg You can add catch handlers to the individual promises and benefit from the short-circuiting behavior.
@possumkeys Жыл бұрын
@@t3dotgg not really, I can't see that on network debugging. It's all strictly required so we start all over again.
@mananbhatia8575 Жыл бұрын
Name that vs code theme please
@calledtigermttv7184 Жыл бұрын
poimandres dark theme
@mananbhatia8575 Жыл бұрын
thanks fr@@calledtigermttv7184
@rudro314 Жыл бұрын
thanks for letting me know how bad js is
@hanes2 Жыл бұрын
if u think async is hard in javascript. well.. it's freaking pain in other languages, even just Python it's quite a nightmare compared to Javascript.
@magisteryura10 ай бұрын
python is a pain as a whole
@truepicksyt3323 Жыл бұрын
But I heard never settle
@bhardwajthummar9313 Жыл бұрын
Hail allsettled() !!!!!
@StephenRayner Жыл бұрын
There’s an opportunity to make all these suggestions accessible. Something a kin to ESLint warnings / recommendations. I’m imagining a plugin that shows brief descriptions of “avoid this” with some if “logical/situation”. Then we the community can add their own, vote, attach KZbin videos. So if you write Promise.all you see this video / a code example.
@avidrucker Жыл бұрын
Love it
@StephenRayner Жыл бұрын
Damn 😮
@DevinBidwell10 ай бұрын
Sorry did you say Exception? I think you mean Error ;)
@tobafett2873 Жыл бұрын
solid vid, good stuff.
@miguelemmara5046 Жыл бұрын
its 'all or nothing'
@aredrih6723 Жыл бұрын
I agree on the correctness side but disagree on the efficiency side. If you need _all_ request to complete to perform a task, exiting early is more efficient. If you want some retry logic, placing it in the catch handler of each individual promise would allow you to retry immediately instead of having to wait on the slowest to resolve first. Also, isn't that be the use case for AbortController ? You tie every request to a signal and as soon as the controller abort, you interrupt as early as possible because the result is no longer required. That said, I remember node logging a warning for memory leak for trying to have more than 10 listener to a signal...
@CottidaeSEA Жыл бұрын
There are cases where all you care about is that the promises are done, not that they succeeded.
@wichtel1805 Жыл бұрын
I have exactly such a case in a current project: It does a few async operations in parallel, all of which must resolve. In case of an error, the shared signal cancels the rest. Works very well since we do not need the individual results in case of an error - and an early result is more valuable than having all error messages should more than one fail. But that's clearly very situation dependent.
@aredrih6723 Жыл бұрын
@@CottidaeSEA catch(() => {}) is always an option if you don't care what kind of error each promise encounters. If you're looking for stat on which action succeeded and which one failed, then yeah, allSettled might fit better. Also, at the end of the day, allSettled is just a promise.all with a well chosen then applied to each promise (e.g. .then((e) => ({e, ok:true}), (e) => ({e, ok:false})))
@CottidaeSEA Жыл бұрын
@@aredrih6723 That is not compatible with await syntax, but yes, that works. Or you could just use allSettled and go on with life.
@IvanRandomDude Жыл бұрын
Thanks F#
@BenRangel Жыл бұрын
4:30 stuff like this contributes to me still using JS instead of TS. Scenarios where your working with some JS code where all the quirks are well known and accepted, but to use it in TS without an error you must do a quirky workaround. It doesn't feel like it adds anything to my code but confusion. I mean, I'd love to use types for the parts of my codebase where it feels significant, but when it affects everything it seems to be more of an annoyance than a feature.
@lapulapucityrider3227 Жыл бұрын
because of small issue you ignore all the benefits sounds an excuse or just being lazy.
@magisteryura10 ай бұрын
lol, i was writing in js since es3 in ie, now i use TS (for 5+ years or so) and i really hate when i need to read plain js code
@d.ilnicki11 ай бұрын
You can still await for any of underlying promise in the array. They aren't going anywhere, references are still here.
@mehulsharmamat Жыл бұрын
Just realized one of my production bugs might be due to EXACTLY this. brb checking
@flatmapper Жыл бұрын
Great
@st-tube Жыл бұрын
Fire and forget is a common pattern so Promise.all is fine but Promise.allSettled is more appropriate in most cases
@ciach0_ Жыл бұрын
I Promise to you Theo that this video is going to be resolved for the all-settled users.
@ymi_yugy3133 Жыл бұрын
For interacting with library code is is a good idea. Thanks for pointing it out. In my own code I don’t use exceptions. So promise.all works fine
@CrzyMan_Personal Жыл бұрын
I think it's more about your mental model. `Promise.all` isn't "wait for all of these to settle", it's more of a "treat these all like one big promise". Once you treat it like that, it makes more sense. after using Supabase a lot, I've become a big fan of my async functions being something akin to `function(...args): Promise`. Where they catch themselves and always resolve nicely. That kind of coalesces the behavior of `Promise.all` and `Promise.allSettled`. Either way, it still forces you to consider the error case a lot better
@yofish101 Жыл бұрын
Yavascript folks…
@96shahab Жыл бұрын
@Theo telemetry is a common place for me. I usually push up data in a fire and forget manner and for some metrics (granted, these are not actually all that many) it's better to use all instead of allSettled just because it allows the publishing function to return faster, reducing the time the function has to run. Those promises in the background can fail and it'll just be treated as a missing data point, which for a lot of metric types is actually not critical.
@NuncNuncNuncNunc Жыл бұрын
How do you know whether or not you have systemic errors? If you have common errors, your data risks being biased.
@96shahab Жыл бұрын
@@NuncNuncNuncNunc it depends on the metric being sent. Something like network tx and rx for example it's okay if publishing fails once every so often. A user may also select a metric to be non essential and that would be treated the same way. Of course, if every publish fails then that's a different matter that we do look out for, though all we do in that case is log to console, as we can't be sure whether the problem is ours or if the user has something in their config or something like a proxy
@mkwpaul Жыл бұрын
Isn't the ^ (caret symbol) bitwise XOR and not exponentiation? From what I can remember ** would be exponentiation.
@neoney Жыл бұрын
he says that in the video
@jkbrodotdev Жыл бұрын
Great video, Promise me you leave the puns out next time
@JAt0m Жыл бұрын
I use this helper here to group data by the 'type' field. Could obviously also be a used with a 'status' field. export type Visitor = { [P in T["type"]]?: Extract[]; }; const groupByType = (data: T[]): Visitor => A.groupBy(data, (item) => item.type) as unknown as Visitor;
@bryanleebmy Жыл бұрын
That `as unknown as Visitor` is a terrible pattern and ugly as hell.