I want to catch error in a callback function.
(async function(){
function wait(ms){
return new Promise(function(resolve, reject){
setTimeout(resolve, ms);
});
}
async function test(){
while(true) {
if (window.err){
throw 'This is an error!';
}
await wait(500);
}
}
try {
console.log('running ...');
window.err = false;
setTimeout(function(){
window.err = true;
}, 1000);
test();
await wait(3000);
console.log('end')
console.log('done')
}
catch(err){
// How do i catch error of "test" function in this
console.log('end')
console.log('error')
}
})()
.................................................................................................................................................................................................... How do i catch error of test function in this? My englist is not good, help me pls! Thanks!