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

2

12

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?

Pleuron answered 4/12, 2017 at 15:41 Comment(2)
Sounds like an issue with the typings. How does ampqplib define the types that its methods return?Sudan
You would get the same error when linting against await Bluebird.resolve(3); with type-checks enabled.Pleuron
S
22

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"]
Sudan answered 5/12, 2017 at 4:13 Comment(2)
Oh that's a very nice find. Why wouldn't they allow it in the microsoft-contrib guidelines if tslint is able to differ between Bluebird and non promises like numbers (as indicated in your comment)?Pleuron
@Pleuron I didn't say that tslint is able to differentiate between promise[-like object]s and non-promises. I just said that the original intent of that thread on Github appears to be about things that aren't promises at all, and as I said, the end conclusion of that thread at least was that the situation was too murky to make any hard rules about it (I assume the current rules were decided elsewhere). Regarding the MS contribution guidelines, it could be that they're not all that keen on allowing non-native promises in their contribs, so wouldn't go out of their way to allow them.Sudan
F
6

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)
);
Fire answered 4/12, 2017 at 15:44 Comment(4)
I am a bit disappointed that Promise.resolve(bluebirdPromsie) would introduce another Promise. But apparently there is no better way to await non native Promises at the moment?Pleuron
@Pleuron await bluebirdPromise is a fine way to await non-native promises. If anything, it's your linter (or TypeScirpt) that's the problem, not the promises.Sudan
@Sudan the tslint error is by design, see: github.com/Microsoft/TypeScript/issues/8310.Pleuron
@Pleuron I don't think that's really what that thread says. Ryan Cavannaugh closed it saying that there's no reliable way to assert that what the user is doing is incorrect. And the whole point of that thread was about people awaiting things that aren't promises of any kind (like 5).Sudan

© 2022 - 2024 — McMap. All rights reserved.