Created
September 15, 2021 18:06
-
-
Save naturallucky/de1e521a9c7d48662048502dc8ce1b9a 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 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