相关文章推荐
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?

Sounds like an issue with the typings. How does ampqplib define the types that its methods return? – JLRishe Dec 4, 2017 at 15:49 You would get the same error when linting against await Bluebird.resolve(3); with type-checks enabled. – kentor Dec 4, 2017 at 15:58

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"]
                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)?
– kentor
                Dec 5, 2017 at 4:29
                @kentor 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.
– JLRishe
                Dec 5, 2017 at 4:42

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)
                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?
– kentor
                Dec 4, 2017 at 16:00
                @kentor 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.
– JLRishe
                Dec 4, 2017 at 20:06
                @JLRishe the tslint error is by design, see: github.com/Microsoft/TypeScript/issues/8310.
– kentor
                Dec 4, 2017 at 22:28
                @kentor 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).
– JLRishe
                Dec 5, 2017 at 4:11
        

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.

 
推荐文章