Skip to content

Instantly share code, notes, and snippets.

@akccakcctw
Created February 24, 2022 10:56
Show Gist options
  • Save akccakcctw/092eb8e3ba88aa93b14b86fed0b2505b to your computer and use it in GitHub Desktop.
Save akccakcctw/092eb8e3ba88aa93b14b86fed0b2505b to your computer and use it in GitHub Desktop.
difference between finally in promise and finally after try..catch
// 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