<!DOCTYPEhtml><script>'use strict';constp=newPromise((resolve,reject)=>{setTimeout(()=>{resolve();},2000);});console.log('waiting');p.then(()=>{console.log('promise is resolved');});</script>
</html>
<!DOCTYPEhtml><script>'use strict';constp=newPromise((resolve,reject)=>{setTimeout(()=>{reject();},2000);});console.log('waiting');p.then(()=>{console.log('promise is resolved');}).catch(()=>{console.log('promise is rejected');});</script>
</html>
resolve / reject with data
<!DOCTYPEhtml><script>'use strict';constp=newPromise((resolve,reject)=>{setTimeout(()=>{// resolve('success!');reject('failure');},2000);});console.log('waiting');p.then(results=>{console.log('promise is resolved');console.log(results);}).catch(results=>{console.log('promise is rejected');console.log(results);});</script>
</html>
constp=newPromise(resolve=>{setTimeout(()=>{console.log('a');resolve('a is done');},2000);});p.then(results=>{console.log(results);returnnewPromise(resolve=>{setTimeout(()=>{console.log('b');resolve('b is done');},2000);});}).then(results=>{console.log(results);console.log('all done');});console.log('waiting...');
<script>'use strict';letdoAsyncCallCounter=0;constdoAsync=(doesResolve,timeout)=>newPromise((resolve,reject)=>{letcurrentCounterValue=++doAsyncCallCounter;setTimeout(()=>{if(doesResolve){console.log('success!',currentCounterValue);resolve({msg:'success!',counter:currentCounterValue});}else{console.log('failure!',currentCounterValue);resolve({msg:'failure!',counter:currentCounterValue});}},timeout);});Promise.race([doAsync(true,2000),doAsync(true,1000)]).then(results=>{console.log('race is over');console.log(results);});</script>