I'm curious if it's possible to use request-promise-native
without
tslint
complaining of
ERROR: .../8ad9dc325b1eea1c0b548120ae05cd6a/index.ts:32:22 - Invalid 'await' of a non-Promise value.
The key is to use .promise()
:
const responsePromise = request(fullOptions)
try {
const value: request.FullResponse = await responsePromise.promise() as request.FullResponse
...
} catch (error) {
...
}
Unfortunately responsePromise.promise()
is of type Promise<any>
so we
still need to coerce it into request.FullResponse
. Also note that the
possible errors are errors.RequestError
and errors.StatusCodeError
(from
'request-promise/errors'
).
Running npm run demo
without any server running on localhost:7711
:
$ npm run demo
> @ demo .../8ad9dc325b1eea1c0b548120ae05cd6a
> tsc && node index.js
[ 'onFailure:RequestError',
{ Error: connect ECONNREFUSED 127.0.0.1:7711
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1104:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 7711 } ]
undefined
and without the server running on localhost:7711
in a state of failure:
$ npm run demo
> @ demo .../8ad9dc325b1eea1c0b548120ae05cd6a
> tsc && node index.js
[ 'onFailure:StatusCodeError', 500 ]
and with the server running correctly:
$ npm run demo
> @ demo .../8ad9dc325b1eea1c0b548120ae05cd6a
> tsc && node index.js
[ 'onSuccess:FullResponse',
{ 'content-type': 'text/html; charset=utf-8',
'content-length': '5',
server: 'Werkzeug/0.14.1 Python/3.7.1',
date: 'Fri, 22 Feb 2019 07:52:58 GMT' },
200,
'hello' ]
undefined