diff --git a/src/libs/API.js b/src/libs/API.js index 83b0dcd9d159..d86fc099a08a 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -105,7 +105,10 @@ function makeRequestWithSideEffects(command, apiCommandParameters = {}, onyxData * @param {Object} [onyxData.failureData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode !== 200. */ function read(command, apiCommandParameters, onyxData) { - makeRequestWithSideEffects(command, apiCommandParameters, onyxData, CONST.API_REQUEST_TYPE.READ); + // Ensure all write requests on the sequential queue have finished responding before running read requests. + // Responses from read requests can overwrite the optimistic data inserted by + // write requests that use the same Onyx keys and haven't responded yet. + SequentialQueue.waitForIdle().then(() => makeRequestWithSideEffects(command, apiCommandParameters, onyxData, CONST.API_REQUEST_TYPE.READ)); } export { diff --git a/src/libs/Network/SequentialQueue.js b/src/libs/Network/SequentialQueue.js index fc670d118405..f37eb8422247 100644 --- a/src/libs/Network/SequentialQueue.js +++ b/src/libs/Network/SequentialQueue.js @@ -114,9 +114,18 @@ function getCurrentRequest() { return currentRequest; } +/** + * Returns a promise that resolves when the sequential queue is done processing all persisted write requests. + * @returns {Promise} + */ +function waitForIdle() { + return isReadyPromise; +} + export { flush, getCurrentRequest, isRunning, push, + waitForIdle, };