Promises
A Promise represents a value that will be available in the future. It is in one of three states: pending, fulfilled, or rejected.
A Promise represents a value that will be available in the future. It is in one of three states: pending, fulfilled, or rejected.
const p = new Promise((resolve, reject) => {
setTimeout(() => resolve("done!"), 1000);
});
p.then(val => console.log(val)) // "done!"
.catch(err => console.error(err))
.finally(() => console.log("always runs"));
Always attach a .catch() — unhandled rejections crash Node and log warnings in browsers.
More in JavaScript