lazy promises
See original GitHub issueI would love promises to be lazy and not get executed on creation - but instead on then
.
I know this is not part of the spec - but I still find it very useful.
Seems like there are others interested in this, too. There is a work-around on SO and someone else created a lib for it.
Would love to hear some thoughts on this.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:17 (2 by maintainers)
Top Results From Across the Web
sindresorhus/p-lazy - GitHub
Create a lazy promise that defers execution until it's awaited or when .then() or .catch() is called - GitHub - sindresorhus/p-lazy: Create a...
Read more >Promise - JavaScript - MDN Web Docs
The Promise object represents the eventual completion (or failure) ... Note: Several other languages have mechanisms for lazy evaluation and ...
Read more >Can I do a "lazy" promise with Bluebird.js? - Stack Overflow
Promises don't work in such a way that they could support lazy loading. Promises are created by asynchronous code in order to communicate...
Read more >Javascript Promises are Eager and Not Lazy - tusharf5.com
Javascript Promises are Eager and Not Lazy. As soon as the javascript Interpreter sees a promise declaration. It immediately executes its implementation ...
Read more >Behold the Power of Laziness With Functional Programming
Promises aren't lazy! As soon as a Promise is instantiated, it executes its callback argument. It's already too late to change its behavior....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t think this would belong anywhere in Bluebird - it’s not really a promise-thing anymore than a general async challenge.
I wrote a package for lazy-execution of an async job (and reuse of it’s promise so that it will never run twice): https://www.npmjs.com/package/smoop#using-promises
Internally it is using Bluebird, but it works with callbacks as well. The async job can be either callback taking or promise returning as well.
Example:
It is pretty simple (63 SLOC) and has options for expiring the result or continuously refreshing it.
@petkaantonov
Not sure I can follow. What’s an “eager promise”? And won’t that also be yet another fresh promise instance on calling it twice?