Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't return falsy value from response handler callback #58

Closed
itsgoingd opened this issue Oct 15, 2019 · 2 comments
Closed

Can't return falsy value from response handler callback #58

itsgoingd opened this issue Oct 15, 2019 · 2 comments
Labels

Comments

@itsgoingd
Copy link

Hey,

From the documentation:

wretch("...").get().json(() => "Hello world!").then(console.log) // Hello world!

This does not work if the returned value is false or null (probably any falsy value), the original request is passed on to the next promise instead.

I took a quick look at the source and I believe it's caused by how the callback is run here

https:/elbywan/wretch/blob/master/src/resolver.ts#L89-L92

I'm not sure if it's an intentional choice, so when the callback does not return you won't lose the original request, or a bug. Even if it's intentional it might be better to explicitly check if the return value is undefined as returning some kind of "success" boolean might be a common use case.

Fortunately the workaround is pretty straight-forward:

wretch("...").get().json().then(() => false).then(console.log)

Thanks for this very nice library btw!

@elbywan elbywan added the bug label Oct 15, 2019
@elbywan
Copy link
Owner

elbywan commented Oct 15, 2019

Hey @itsgoingd,

This does not work if the returned value is false or null (probably any falsy value)

Good catch!

I'm not sure if it's an intentional choice, so when the callback does not return you won't lose the original request, or a bug.

It is indeed a bug, as I have the tendency to overlook the fact that null and false are perfectly valid json values 🤦‍♂.

From looking at the code, when the server returns a falsey json - or when the .json callback returns a falsey value then something wrong (original response or parsed body - depending on the case) will be forwarded to the next promise resolver in the chain.

I'll investigate and fix it later today, thanks for reporting the issue! 👍

@elbywan
Copy link
Owner

elbywan commented Oct 15, 2019

Just released v1.5.5 with the fix. 📦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants