yahoo / fetchr

Universal data access layer for web applications.
Other
453 stars 86 forks source link

fix: always return the same value when resolving requests multiple times #522

Closed pablopalacios closed 2 months ago

pablopalacios commented 2 months ago

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.

I found a bug in our system when resolving a request multiple times. The first time it was resolved it was fine (and so fetchr tests), but at the second time it would resolve with an undefined value. The code was looking something like this:

const request = fetchr.read('foo', { bar: 42 }); // request was triggered
track(request); // would call then internally
return request; // the consumer of this function would also call then

Fixing it was simple, we just had to keep a reference to the fetch promise in httpRequest and always return _request.then in FetchrHttpRequest.then method.

However, that broke the abort support since we were not returning an instance of FetchrHttpRequest anymore to the caller. To solve that, it was also necessary to store a reference to FetchrHttpRequest in the Request class in fetchr.client.js.

I added a bunch of more tests to cover this and other cases as well.