Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Created October 31, 2017 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngyuki/23063b7691161655f94e0c237dda9894 to your computer and use it in GitHub Desktop.
Save ngyuki/23063b7691161655f94e0c237dda9894 to your computer and use it in GitHub Desktop.
function success(t){
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, t);
})
}
function error(t){
return new Promise((resolve, reject) => {
setTimeout(() => {
reject();
}, t);
})
}
/**
- [A] と [B] を開始する
- [B] が終わったら [C] を開始する
- [A] と [C] が終わったら [D] を実行する
- どこかでエラーが発生したら処理 [E] を実行する
**/
(function(){
const t = new Date();
const e = error(150); // [A]
return success(100) // [B]
.then(()=>{
return success(100); // [C]
})
.then(()=>{
return e;
})
.then(()=>{
// [D]
console.log('done', new Date() - t);
})
.catch(()=>{
// [E]
console.log('error', new Date() - t);
})
}())
/**
(node:13206) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): undefined
(node:13206) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
error 204
(node:13206) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment