elbywan / wretch

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

Chain after resolver #81

Closed xpuu closed 4 years ago

xpuu commented 4 years ago

I have to connect to an API, which is constantly regenerating its authentication token. I thought it'd be nice use case for a resolver. But it doesn't work as expected, because get() in this case doesn't return a promise. Would you be so kind and correct my dellusions? Here's is the fiddle.

let w = wretch('https://reqres.in/api/users/2').resolve(resolver => {
  resolver.res(res => {
    output(Array.from(res.headers.entries()))
    return res.json()
  })
})

w.get().then(res => output(res))

P.S. Many thanks for your lovely little library.

elbywan commented 4 years ago

Hi @xpuu,

P.S. Many thanks for your lovely little library.

❤️

But it doesn't work as expected, because get() in this case doesn't return a promise. Would you be so kind and correct my dellusions? Here's is the fiddle.

Sure, I think you forgot to return the resolver.

let w = wretch('https://reqres.in/api/users/2').resolve(resolver => {
  // Missing "return" keyword - or remove the curly braces:
  return resolver.res(res => {
    output(Array.from(res.headers.entries()))
    return res.json()
  })
})

w.get().then(res => output(res))