question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tjconceptcommented, Oct 27, 2015

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:

var smoop = require('smoop');

var myQuery = smoop(function(){
    return queryTheDatabase();
});

// return now and queryTheDatabase is never called

myQuery().then(function( result ){
    console.log(result);
});

// queryTheDatabase is called

It is pretty simple (63 SLOC) and has options for expiring the result or continuously refreshing it.

0reactions
tcurdtcommented, Dec 14, 2016

@petkaantonov

Instead of passing a lazy promise, you can try passing a function that returns an eager promise which has the same effect.

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found