TIL Promise Constructor edge-case
POSTED ON:
TAGS: javascript async
// Creating a promise with the promise constructor
// this will also catch the error
const createdPromise = new Promise((resolve, reject) => {
somePreviousPromise.then(result => {
// do something with the result
resolve(result);
}).catch(reject);
});
// This is the same function, only cleaner
const createdPromise = somePreviousPromise.then(result => {
// do something with result
return result;
});
The promise constructor has to do a few more workarounds in order to catch errors.
Via 3 most common mistakes when using Promises in JavaScript
Related TILs
Tagged: javascript