Skip to content

Instantly share code, notes, and snippets.

@naturallucky
Created September 15, 2021 18:06
Show Gist options
  • Save naturallucky/de1e521a9c7d48662048502dc8ce1b9a to your computer and use it in GitHub Desktop.
Save naturallucky/de1e521a9c7d48662048502dc8ce1b9a to your computer and use it in GitHub Desktop.
function resolveAfter2Seconds() {
//main objective
let taskHandler = function (thisResfuncArg,denyFuncArg){
// 6
// do something ....
// many many
// yeah , many many
if (false){
//success
thisResfuncArg('resolved');
}else{
denyFuncArg(" what a fxxx!");
}
};
//"jsから resolve用関数を引数で渡される"ので、同期して行いたいことが終わったら結果を受けて、それに処理を返す。
let resFunc = function(resFuncArg,denyFuncArg) {
//5
setTimeout(taskHandler , 2000 , resFuncArg,denyFuncArg);
//上記 taskHandler(resFuncArg,denyFuncArg)を2秒後に 呼ぶ
//すぐなら、 taskHandlerの内容をここに書いてもいい。
// resfuncArg('resolved');を2秒後に実施するという意味
};
//4
let p = new Promise(resFunc);
return p;
}
async function asyncCall() {
//別スレッド(のようなものだと思う)
//2.
console.log('calling');
//3
//待ちが入りますぅ。
const result = await resolveAfter2Seconds().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:");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment