Skip to content

Instantly share code, notes, and snippets.

@naturallucky
Last active September 15, 2021 18:21
Show Gist options
  • Save naturallucky/48609c77b09213cd9c02aacf0d6a8578 to your computer and use it in GitHub Desktop.
Save naturallucky/48609c77b09213cd9c02aacf0d6a8578 to your computer and use it in GitHub Desktop.
//アロー関数、レキシカルクロージャを使わないバージョン
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環境下)。
@naturallucky
Copy link
Author

output:

calling
main end:
too bad
NG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment