我正在使用“async”函数,有时我忘记为内部调用添加“await”:
async function doThreeSteps () => {
await firstPromiseReturningFunc();
nonAsyncFunction();
secondPromiseReturningFunc(); //Error here! Need to wait until this call resolves
someOtherStepWhichReliesOnSuccessfullEndingOfPreviousFunction();
}
TypeScript 应该可以警告此类错误,因为 TS 知道每个函数返回什么类型,是 promise 还是 value。 我想让 TS 警告我这种情况。可能吗?
最佳答案
TSLint ( https://github.com/palantir/tslint ) 具有“无 float promise ”规则。它要求某事通过函数返回的 Promise 完成,例如 .then、.catch、await、assignment 或 return。
还有一个 TypeScript 问题:https://github.com/Microsoft/TypeScript/issues/13376
关于 typescript ,自动等待所有返回 promise 的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42748342/