americanexpress / americanexpress/fetchye

In flight Cache hits in oneFetchye (or the underlying makeServerFetchye) don't return a shared promise

Open
#93 1 comment 0 reactions 0 assignees View on GitHub
stale-issue
Dominant language
JavaScript
Stars
46
Forks
27
PR merge metrics
No merged PRs in 30d

Description

# 🐞 Bug Report

## Describe the bug

If two requests are made to the same data, if the second request is made while the first request is still "in flight" it will return both "error" and "data" as undefined instead of the promise to the inflight request.

This is because during a second call to the same data, while the first call is 'in flight':

This block will be skipped, because we _do_ have a cacheSelector via `oneFetchye`:
https://github.com/americanexpress/fetchye/blob/main/packages/fetchye/src/makeServerFetchye.js#L42

This block will be skipped, because the request _is_ `loading`:
https://github.com/americanexpress/fetchye/blob/main/packages/fetchye/src/makeServerFetchye.js#L54

The function will then "syncronously" return the `data`, `error`, and `run` fields:
https://github.com/americanexpress/fetchye/blob/main/packages/fetchye/src/makeServerFetchye.js#L64

However `data` and `error` will be undefined because the original request is still in flight.

## To Reproduce

Define two sequenced functions (in psuedo code):

```
const getUserPlaylists = (dispatch) => {
const { data: token } = await dispatch(oneFetchye(".../logIn"));
const { data: playlists } = await dispatch(oneFetchye(".../readUserPlaylists"));
return playlists;
}

const getUserPreferences = (dispatch) => {
const { data: token } = await dispatch(oneFetchye(".../logIn"));
const { data: preferences } = await dispatch(oneFetchye(".../readUserPreferences"));
return preferences;
}
```

Then race them:
```
return Promise.all([dispatch(getUserPlaylists), dispatch(getUserPreferences)]);
```

Note: This bug is also present if you are calling for the same data from multiple one-app modules.

Whichever call to `logIn` happens _second_ will "synchronously" receive `undefined` as the `token`.

## Expected behavior

Both calls to get the token properly return a promise to the data from the single call made

Contributor guide

Open the contributing guide

Research direction

Start in packages/fetchye/src/makeServerFetchye.js at the cacheSelector, loading, and return paths around lines 42, 54, and 64, then trace how oneFetchye dispatches concurrent requests. Reproduce the sequenced login race from the issue and verify that both calls receive the in-flight request's data instead of undefined.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.