Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active February 4, 2024 18:06
Show Gist options
  • Save nolanlawson/6ce81186421d2fa109a4 to your computer and use it in GitHub Desktop.
Save nolanlawson/6ce81186421d2fa109a4 to your computer and use it in GitHub Desktop.
Promise protips - stuff I wish I had known when I started with Promises
// Promise.all is good for executing many promises at once
Promise.all([
promise1,
promise2
]);
// Promise.resolve is good for wrapping synchronous code
Promise.resolve().then(function () {
if (somethingIsNotRight()) {
throw new Error("I will be rejected asynchronously!");
} else {
return "This string will be resolved asynchronously!";
}
});
// execute some promises one after the other.
// this takes an array of promise factories, i.e.
// an array of functions that RETURN a promise
// (not an array of promises themselves; those would execute immediately)
function sequentialize(promiseFactories) {
var chain = Promise.resolve();
promiseFactories.forEach(function (promiseFactory) {
chain = chain.then(promiseFactory);
});
return chain;
}
// Promise.race is good for setting a timeout:
Promise.race([
new Promise(function (resolve, reject) {
setTimeout(reject, 10000); // timeout after 10 secs
}),
doSomethingThatMayTakeAwhile()
]);
// Promise finally util similar to Q.finally
// e.g. promise.then(...).catch().then(...).finally(...)
function finally (promise, cb) {
return promise.then(function (res) {
var promise2 = cb();
if (typeof promise2.then === 'function') {
return promise2.then(function () {
return res;
});
}
return res;
}, function (reason) {
var promise2 = cb();
if (typeof promise2.then === 'function') {
return promise2.then(function () {
throw reason;
});
}
throw reason;
});
};
@Jeff-Walker
Copy link

@nolanlawson
Sorry if I've missed the point, but finally() isn't a function on the Promise type, so the example in the comment doesn't work. So, how do you use it? Wrap the whole thing in the function call? Kinda kills the beauty of the promise chain. Add it to the Promise prototype?

Thanks.

@owendall
Copy link

Great Article!

@ridespirals
Copy link

ridespirals commented Oct 31, 2017

Here's a tip for if you want to use Promise.all but want something nicer than an array to hold the final values:

Promise.all({
    foo: doSomething(),
    bar: doSomethingElse(),
    quux: doAnotherThing().then(q => q.property)
}).then(values => {
    const { foo, bar, quux } = values
    // now you have the result of foo, bar, and quux (including the transformation on quux from the "then" that it had)
})

@goyanx
Copy link

goyanx commented Feb 3, 2018

nice article!

@JestVA
Copy link

JestVA commented Jan 13, 2019

Great article, still a rookie to understand promises and make useful application of the concepts.

@xgqfrms
Copy link

xgqfrms commented Nov 16, 2020

👍 awesome promise article

@delbaredouard
Copy link

delbaredouard commented Jan 17, 2022

Thanks for that :-D

putYourRightFootIn()
  .then(putYourRightFootOut)
  .then(putYourRightFootIn)  
  .then(shakeItAllAbout);

Taken from your enlightening easy to read blog post.

@JanithaR
Copy link

What's a good use case for wrapping synchronous code with a Promise?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment