6 ms·
In Javascript you can make anything into a lazily evaluated expression by wrapping it into a function (sometimes called a "thunk"). // eager new Promise(resol
by bernawil 7y ago
In Javascript you can make anything into a lazily evaluated expression by wrapping it into a function (sometimes called a "thunk").
// eager
new Promise(resolve => resolve(foo))
// lazy
const bar = () => new Promise(resolve => resolve(foo))
An executor would be something that takes n functions that return Promises and calls them.
- steveklabnik 7y agoSure, we do this in Rust too. I’m not sure what benefit this would bring you in JS.
- bernawil 7y agoIt's most useful with anything sending multiple requests. Most often you have all the data to perform all requests beforehand, but you can't just fetch (or whatever xmlHttpRequest abstraction you like) to send all requests at once eagerly because of: performance in the browser (running something like 10 simultaneous ajax requests in a single tab often can really lag it) and rate limiting in the back end. So it's better to prepare all requests wrapped in a thunk and then you can execute them at will. You can really optimize as you like, like running some in parallel, secuentially or in batches. Of course this is for really custom cases, more often than not you are best served by something like Promise.map in bluebird which gives you a concurrency option. (http://bluebirdjs.com/docs/api/promise.map.html http://bluebirdjs.com/docs/api/promise.map.html).
- steveklabnik 7y agoGotcha, thanks!