4 ms·
The simple answer is that there’s lots of instances in which it’s useful to interact with promises, instead of immediately await-ing them. Some instances off th
by josephg 4y ago
The simple answer is that there’s lots of instances in which it’s useful to interact with promises, instead of immediately await-ing them. Some instances off the top of my head:
- Promise.all lets you do work in parallel. Very useful if you have a set of network requests to make, or things like that
- Sometimes I want multiple parts of my program to respond to some event happening. You can use a promise as a simple one shot event dispatcher. Eg:
this.p = fetch(…)
… then elsewhere, from multiple call sites:
foo.p.then(…)
You can use this for http requests, user actions (make a promise which resolves when the user closes the form) or for dynamically importing a module. I’ve used this a lot with for webassembly modules in the browser. Multiple bits of code can all wait for the module to load and then start using it as soon as it’s ready. And once it’s ready, the promise will resolve immediately.
I’m sure there are other uses, but these are things I’ve used raw promises for. They’re handy!