Promises
A Promise is an object that may produce a single value sometime in the future, either a resolves value or a reason that it's not resolved (rejected).
EX:
const promise = new Promise((resolve, reject) => {
if (true) {
resolve("Stuff worked");
} else {
reject("Error: Something went wrong");
}
});
promise.then(res => console.log(res)); // Stuff worked
promise
.then(res => res + '!')
.then(res2 => console.log(res2)); // Stuff worked!
promise
.then(res => res + '!')
.then(res2 => {
throw Error('Simple error');
console.log(res2);
})
.catch((err) => console.log(err)); // Simple error
promise
.then(res => res + '!')
.then(res2 => {
return res2 + '?';
})
.catch((err) => console.log(err))
.then((res3) => console.log(res3)); // Stuff worked!?Last updated