kentor
kentor

Reputation: 18504

Invalid await in RequestPromise

I've got a TSLint error which says "Invalid 'await' of a non-Promise value." for the following line:

const response: RequestResponse = <RequestResponse>await this.apiRequest(uri);

Context code:

private apiRequest: RequestAPI<request.RequestPromise, request.RequestPromiseOptions, RequiredUriUrl>;
this.apiRequest = request.defaults({
      baseUrl: 'https://www.google.com/',
      method: 'GET',
      encoding: 'utf8'
    });

According to the Type definitions the return type for this.apiRequest(uri) is request.RequestPromise. RequestPromise again is defined like this in the @types/request-promise library:

interface RequestPromise extends request.Request, Promise<any> {
      promise(): Promise<any>;
}

Shouldn't it be possible to await the RequestPromise then since it just extends a Promise?

Upvotes: 1

Views: 642

Answers (1)

kentor
kentor

Reputation: 18504

The reason why this does not work is that request-promise is using Bluebird as Promise library. Tslint is bleating because you are awaiting a Bluebird promise and not a native promise.

At the top of @types/request-promise you will find the Promise override

import Promise = require('bluebird');

In order to get rid of the tslint warning you can either use the request library using native promises (request-promise-native) or simply use .then() instead of await to wait for the resolved response.

Upvotes: 1

Related Questions