elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.83k stars 98 forks source link

TypeError when using timeout with node-fetch polyfill #86

Closed xpuu closed 4 years ago

xpuu commented 4 years ago

This code (running in node v14.4.0)

import fetch from 'node-fetch'
import wretch from 'wretch'

wretch('http://foobar.baz').polyfills({ fetch }).get().setTimeout(1)

results with

TypeError: Cannot read property 'abort' of undefined
    at Timeout._onTimeout (/wretch/node-fetch/node_modules/wretch/dist/bundle/wretch.js:1:6101)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)

Any idea what's wrong? Thanks in advance.

elbywan commented 4 years ago

Hi @xpuu,

node-fetch needs an AbortController polyfill to support that feature. (as written in the documentation)

const fetch = require('node-fetch')
const AbortController = require('abort-controller')
const wretch = require('wretch')

wretch('https://duckduckgo.com')
    .polyfills({ fetch, AbortController })
    .get()
    .setTimeout(1)
    .text(console.log)
// -> AbortError: The user aborted a request.

More info here.

xpuu commented 4 years ago

Wow, that was quick! Once again thanks for your support.