如何拒绝异步/等待功能返回的承诺?
例如本来
foo(id: string): Promise<A> {
return new Promise((resolve, reject) => {
someAsyncPromise().then((value)=>resolve(200)).catch((err)=>reject(400))
});
}
转换为异步/等待
async foo(id: string): Promise<A> {
try{
await someAsyncPromise();
return 200;
} catch(error) {//here goes if someAsyncPromise() rejected}
return 400; //this will result in a resolved promise.
});
}
那么,在这种情况下,我如何才能正确拒绝这一承诺?
Promise
构造函数反模式!甚至第一个摘录也应该被写成foo(id: string): Promise<A> { return someAsyncPromise().then(()=>{ return 200; }, ()=>{ throw 400; }); }