Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid 'await' of a non-Promise value (Bluebird promise)

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?

like image 416
kentor Avatar asked Dec 04 '17 15:12

kentor


2 Answers

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)
);
like image 71
Fathy Avatar answered Oct 20 '22 01:10

Fathy


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"]
like image 32
JLRishe Avatar answered Oct 20 '22 01:10

JLRishe