Typescript,自动等待所有Promise返回函数调用 [英] Typescript, auto await all Promise-returning function calls

查看:145
本文介绍了Typescript,自动等待所有Promise返回函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用异步"功能,有时我忘记为内部通话添加等待":

I am using "async" function and sometimes I am forgetting to add "await" for calls inside:

async function doThreeSteps () => {
    await firstPromiseReturningFunc();
    nonAsyncFunction();
    secondPromiseReturningFunc(); //Error here! Need to wait until this call resolves
    someOtherStepWhichReliesOnSuccessfullEndingOfPreviousFunction();
}

因为TS知道每个函数返回的类型是诺言还是值,所以TypeScript应该警告此类错误.我想让TS对此类情况发出警告.有可能吗?

It should be possible for TypeScript to warn about such error because TS knows what type does each function returns, is it promise or value. I'd like to make TS to warn me about such cases. Is it possible?

推荐答案

TSLint( https://github.com/palantir/tslint )具有无浮动承诺"规则.它要求使用从.then,.catch,await,赋值或返回之类的函数返回的Promise完成某事.

TSLint (https://github.com/palantir/tslint) has the "no-floating-promises" rule. It requires that something be done with a Promise returned from a function such as .then, .catch, await, assignment or return.

对此还有一个TypeScript问题: https://github.com/Microsoft/TypeScript/issues/13376

There's also a TypeScript issue for this: https://github.com/Microsoft/TypeScript/issues/13376

这篇关于Typescript,自动等待所有Promise返回函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆