Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

Functions rescue us in this case because functions are lazy. But now, we can’t chain these things with .then() anymore.

function lazyPromise() {
  return new Promise(resolve => setTimeout(resolve, 1000))
    .then(() => console.log('Chain off all'));
// The only location you can't chain off is the function

// Chains just fine this way
lazyPromise().then(() => console.log('Chain off one'));
lazyPromise().then(() => console.log('Chain off two'));
const getUserAge = () => betterFetch('')
  .then(res => res.json())
  .then(user => user.age);

And then to use this thing, we would call Call run multiple times and you will invoke multiple different executions of that chain of eventual values. Yay! Reusable but still chainable.

How is this any different from what is shown below?

// Lazy and chainable
getUserAge().then(age => {});
getUserAge().then(age => {});

The post references chainability many times and I'm still confused as to when you are not able to chain using a Promise getter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.