How to use Promise.prototype.finally() in async/await syntax?
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`);
}
}
Related videos on Youtube
AmerllicA
I'm a Frontend Engineer in a country under US economic sanction, I cannot earn like other developers in the world, I cannot have a Master or Visa card or even PayPal, applying from Stack Overflow for Job Opportunities will be banned because my country is in the Black list for every action. I cannot use Upwork, LinkedIn premium features, or any website you think. Donation crypto wallet, USDT (TRC20 network): TJJUKR9LSsU5j4rE9J9nZxJxQypoLWkaC4
Updated on July 09, 2022Comments
-
AmerllicA almost 2 years
Actually my main question was using
Promise.prototype.catch()
inasync/await
ES8
syntax, UndoubtedlyPromise.prototype.then()
is existed in essence ofasync/await
syntax.I searched about using
Promise.prototype.catch()
inasync/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
inasync/await
syntax?