Last active
September 15, 2021 18:21
-
-
Save naturallucky/48609c77b09213cd9c02aacf0d6a8578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//アロー関数、レキシカルクロージャを使わないバージョン | |
function taskManager(){ | |
//"jsから resolve用関数を引数で渡される"ので、同期して行いたいことが終わったら結果を受けて、それに処理を返す。 | |
let taskHandler = function(resFuncArg,denyFuncArg) { | |
//5 | |
// do something .... | |
// many many | |
// yeah , many many | |
// 6 | |
if (false){ | |
//success | |
resFuncArg('resolved'); | |
}else{ | |
denyFuncArg(" what a fxxx!"); | |
} | |
}; | |
//4 | |
let p = new Promise(taskHandler); | |
return p; | |
}; | |
async function asyncCall() { | |
//別スレッド(のようなものだと思う) | |
//2. | |
console.log('calling'); | |
//3 | |
//待ちが入りますぅ。 | |
// asyncの中で awaitを使う | |
const result = await taskManager().catch(e => { | |
console.log("too bad"); | |
return "NG";//some value or Promise | |
}); | |
console.log(result); | |
// expected output: "resolved" | |
} | |
//1. | |
asyncCall(); | |
//2. | |
console.log("main end:"); | |
//awaitの方式は | |
//async , await , promise , resolve/deny 処理 , 本処理と 5段階踏むので、 | |
//setTimeout()の方がちょっとした事は簡単(client環境下)。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output:
calling
main end:
too bad
NG