https://scotch.io/tutorials/javascript-promises-for-dummies https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261
const obj = {
isTrue: true,
isFalse: false
}
const secondPromise = finalMsg => {
const final = `${finalMsg} in the second promise`;
return Promise.resolve(final);
}
const promise = new Promise((resolve, reject) => {
const { isTrue, isFalse } = obj;
if (isTrue) {
const message = `This is ${isTrue}`;
resolve(message);
} else {
const message = `This is ${isFalse}`;
reject(message);
}
})
promise.then(secondPromise).then(success => {
console.log("Success", success)
}).catch(error => {
console.log("Error", error)
})
What is a promise?
An object that may produce a single value in the future
Three states
The ES6 Promise constructor takes a function, which takes in two parameters:
resolve()
reject()