got: Promise not resolving after response successfully received

Hey, I have a tricky little issue I wanted to ask about. I’ve found myself in a weird little situation where got is successfully fetching a 200 response from a server and calling resolve(response) with the response body correctly at the bottom of as-promise.js, but the data for some reason never makes it back to the call site.

I’ve prepared a little example repo which reproduces the issue, and the relevant code is quoted below.

import * as got from 'got'

export async function onPostBuild(): Promise<void> {
  const promise = got('http://example.org')
  console.log(promise)
  setTimeout(() => console.log(promise), 2000)

  const html = await got('http://example.org')
  console.log(html)
}

After the two second timeout, the promise is visibly resolved in the console.log output.

console.log(promise) setTimeout(() => console.log(promise), 2000)
screenshot 2019-01-17 at 15 40 40 screenshot 2019-01-17 at 15 42 12

However, for some reason, the await got() version in the 2nd block of code hangs forever. The data has been fetched but for some reason the flow of control breaks down while returning it back up the stack to my function. I’ve tried .then() as well and had no luck with that either.

Is there some obvious mistake in my usage of got here? I’ve been debugging this for at least two hours now and figured at this point it’s probably helpful to have the question up for the sake of the future Google results even if I’m doing something really silly! 😇

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 22

Most upvoted comments

I’m seeing a similar behavior when using Got in our TypeScript project. Same as shown above, using const res = await got("example.com"); yields no result, but hangs indefinitely. On the contrary to @hensmith though, calling .then() did return a response as expected, just awaiting the returned promise somehow failed.

I’m seeing the issues in a project using: Node: v8.15.0 Got: v9.6.0 TypeScript: v2.9.2 @types/got: v9.4.0 bluebird: v3.5.0

Our TypeScript target is es5 using the commonjs module, similar to the example code provided above. Promises are polyfilled globally using bluebird.

Alright, I’ll try to set up a small sample to reproduce it tomorrow or on Sunday 👍 Will post back here with results.

I’d personally try to avoid it and using await if possible.

That’s not a good behavior though…

I believe it’s Bluebird failure. I’ll let you know when I have figured out what’s wrong 😃