When I tslint my whole project using tslint --project tsconfig.json src/**/*.ts
I get lots of tslint errors like these:
Invalid 'await' of a non-Promise value.
These errors pops up in every line where I am awaiting a Bluebird promise. I wonder what I should do to avoid these warnings? At runtime I don't face any issues, however I assume that there is a good reason to fix these issues?
For instance I am using the amqplib library which uses Bluebird for all promises. And every time I await one of the promise based methods I will get a tslint error:
const queueInfo: Replies.AssertQueue = await this.channel.assertQueue(this.jobQueueName);
Question:
What is the best way for awaiting non-Promise values like Bluebird promises?
You can convert any "thenable" object (with a least a then()
method) to a native Promise
using Promise.resolve
.
const queueInfo: Replies.AssertQueue = await Promise.resolve(this.channel.assertQueue(this.jobQueueName));
Alternative syntax (a little bit less efficient because of the closure) :
const queueInfo: Replies.AssertQueue = await Promise.resolve().then(() =>
this.channel.assertQueue(this.jobQueueName)
);
It looks like TSLint contains a setting for indicating which types to treat as promises in await
expressions:
https://palantir.github.io/tslint/rules/await-promise/
I haven't tried this myself, but it looks like you should be able to use this to allow awaiting Bluebird promises:
"await-promise": [true, "Bluebird"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With