无效'等待'非承诺值(蓝鸟承诺)

ken*_*tor 9 node.js typescript bluebird tslint

当我使用我的整个项目时,tslint --project tsconfig.json src/**/*.ts我得到了很多像这样的tslint错误:

非承诺值的'await'无效.

在我等待Bluebird承诺的每一行中都会弹出这些错误.我想知道我应该做些什么来避免这些警告?在运行时我不会遇到任何问题,但我认为有充分的理由解决这些问题?

例如,我正在使用amqplib库,它使用Bluebird进行所有承诺.每当我等待一个基于promise的方法时,我都会遇到一个tslint错误:

const queueInfo: Replies.AssertQueue = await this.channel.assertQueue(this.jobQueueName);
Run Code Online (Sandbox Code Playgroud)

题:

等待Bluebird承诺的非承诺价值的最佳方法是什么?

JLR*_*she 19

看起来TSLint包含一个设置,用于指示在await表达式中将哪些类型视为promises :

https://palantir.github.io/tslint/rules/await-promise/

我自己没有试过这个,但看起来你应该能够使用它来等待Bluebird的承诺:

"await-promise": [true, "Bluebird"]
Run Code Online (Sandbox Code Playgroud)


fat*_*thy 5

您可以使用将任何“thenable”对象(至少有一个then()方法)转换为本机。PromisePromise.resolve

const queueInfo: Replies.AssertQueue = await Promise.resolve(this.channel.assertQueue(this.jobQueueName));
Run Code Online (Sandbox Code Playgroud)

替代语法(由于闭包,效率稍低)

const queueInfo: Replies.AssertQueue = await Promise.resolve().then(() =>
    this.channel.assertQueue(this.jobQueueName)
);
Run Code Online (Sandbox Code Playgroud)