Created
February 24, 2022 10:56
-
-
Save akccakcctw/092eb8e3ba88aa93b14b86fed0b2505b to your computer and use it in GitHub Desktop.
difference between finally in promise and finally after try..catch
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
// https://dev.to/annarankin/finally-in-promises--trycatch-2c44 | |
async function fnA() { | |
try { | |
await sleep(300); | |
console.log('fnA success'); | |
return 'yooo'; | |
} catch { | |
console.error('fnA error'); | |
} finally { | |
console.log('fnA finally'); | |
console.log('-----------') | |
return 'haha'; | |
} | |
} | |
function fnB() { | |
return sleep(300) | |
.then(() => { | |
console.log('fnB success'); | |
return 'yooo'; | |
}) | |
.catch(() => { | |
console.error('fnB error'); | |
}) | |
.finally(() => { | |
console.log('fnB finally'); | |
console.log('-----------') | |
return 'haha'; | |
}); | |
} | |
function sleep(second) { | |
console.log('start to sleep...'); | |
return new Promise((resolve, _reject) => { | |
setTimeout(() => { | |
console.log('sleep over'); | |
return resolve(); | |
}, second); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment