constpromise=newPromise((resolve,reject)=>{constres=true;// An asynchronous operation.if(res){resolve("Resolved!");}else{reject(Error("Error"));}});promise.then((res)=>console.log(res),(err)=>console.error(err),);
constpromise=newPromise((resolve,reject)=>{setTimeout(()=>{resolve("*");},1000);});consttwoStars=(star)=>{returnstar+star;};constoneDot=(star)=>{returnstar+".";};constprint=(val)=>{console.log(val);};// Chaining them all togetherpromise.then(twoStars).then(oneDot).then(print);
Creating
constexecutorFn=(resolve,reject)=>{console.log("The executor function of the promise!");};constpromise=newPromise(executorFn);
Chaining multiple .then()
constpromise=newPromise((resolve)=>setTimeout(()=>resolve("dAlan"),100));promise.then((res)=>{returnres==="Alan"?Promise.resolve("Hey Alan!"):Promise.reject("Who are you?");}).then((res)=>{console.log(res);},(err)=>{console.error(err);},);