实际上我的主要问题是使用Promise.prototype.catch()
in async/await
ES8
语法,毫无疑问Promise.prototype.then()
本质上是存在的async/await
syntax.
我搜索了有关使用Promise.prototype.catch()
in async/await
并发现了这个:
async () => {
try {
const result1 = await firstAsynchronousFunction();
const result2 = await secondAsynchronousFunction(result1);
console.log(result2);
} catch(err) {
throw new Error(`Something failed`);
}
}
我绝对知道Promise
链接,例如:
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`);
})
所以,我的问题是我该如何使用finally
in async/await
syntax?
这应该有效:
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`);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)