Monday, May 20, 2024
179
rated 0 times [  180] [ 1]  / answers: 1 / hits: 32243  / 6 Years ago, wed, may 16, 2018, 12:00:00

Actually my main question was using Promise.prototype.catch() in async/await ES8 syntax, Undoubtedly Promise.prototype.then() is existed in essence of async/await syntax.



I searched about using Promise.prototype.catch() in async/await and found this:



async () => {
try {
const result1 = await firstAsynchronousFunction();
const result2 = await secondAsynchronousFunction(result1);
console.log(result2);
} catch(err) {
throw new Error(`Something failed`);
}
}


And absolutely I knew about Promise chaining, like:



new Promise((resolve) => {
console.log(`Initial`);
resolve();
})
.then(() => {
console.log(`Task Number One`);
})
.catch(() => {
console.log(`Task in Error`);
})
.finally(() => {
console.log(`All Tasks is Done`);
})


So, my question is how I can use finally in async/await syntax?


More From » asynchronous

 Answers
13

this should work:


async () => {
try {
const result1 = await firstAsynchronousFunction();
const result2 = await secondAsynchronousFunction(result1);
console.log(result2);
} catch(err) {
throw new Error(`Something failed`);
} finally {
console.log(`All Tasks are Done`);
}
}

[#54420] Saturday, May 12, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;