Ame*_*icA 12 javascript asynchronous async-await ecmascript-2017
实际上我的主要问题是Promise.prototype.catch()在async/await ES8语法中使用,毫无疑问在语法的Promise.prototype.then()本质上存在async/await.
我搜索有关使用Promise.prototype.catch() 中async/await,发现了这个:
async () => {
try {
const result1 = await firstAsynchronousFunction();
const result2 = await secondAsynchronousFunction(result1);
console.log(result2);
} catch(err) {
throw new Error(`Something failed`);
}
}
Run Code Online (Sandbox Code Playgroud)
绝对我知道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`);
})
Run Code Online (Sandbox Code Playgroud)
那么,我的问题是如何finally在async/await语法中使用?
jcu*_*bic 20
这应该工作:
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 is Done`);
}
}
Run Code Online (Sandbox Code Playgroud)