/* * Promises have three states: Pending, Resolved or Rejected * They execute immediately and settle into a final state * Anything that runs `then` on a promise will get the data * from the Resolved or Rejected state */ let done = true // execution of the promise begins immediately const isItDoneYet = new Promise((resolve, reject) => { console.log("new Promise") if (done) { const workDone = 'Here is the thing I built' resolve(workDone) } else { const why = 'Still working on something else' reject(why) } console.log("end promise") }) const checkIfItsDone = () => { isItDoneYet .then(ok => { console.log(`then: ${ok}`) }) .catch(err => { console.error(`catch: ${err}`) }) } // Note how these both get the resolved data checkIfItsDone() setTimeout(checkIfItsDone, 1000)