[英]How to return promise with modifications from async function?
我有一个异步 function,我需要在这个 function 中执行以下操作:
这就是我所做的:
public async foo(): Promise<B> {
const promiseA: Promise<A> = ajaxRequest(...);
promiseA.then((a) => {
return new Promise<B>((resolve) => {
const b: B = process(a);
resolve(b);
})
});
promise.catch(e => {
console.error(e);
return new Promise<B>((resolve) => {
resolve(null);
})
})
}
我得到A function whose declared type is neither 'void' nor 'any' must return a value.
编译错误。 谁能说怎么做?
您的 function 似乎简化为
class Something {
public async foo(): Promise<B | null> {
try {
const a = await ajaxRequest(...);
return process(a);
} catch (e) {
console.error(e);
return null;
}
}
}
假设process
返回B
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.