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('https://my.api.lol/user/295712')
.then(res => res.json())
.then(user => user.age);
And then to use this thing, we would call getUserAge.run(cb). 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.