Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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?
–
–
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"]
–
–
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)
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.