2022 第25週 學習筆記:在 React App 中對應 race condition

總結

最近撞到 race condition 的坑,記錄一下在使用 react 搭配 axios 開發時可以怎麼解決

筆記

情境描述

解法

關於 race condition

Wikipedia: A race condition (or race hazard) is the condition of an electronics, software, or other system where the system’s substantive behavior is dependent on the sequence or timing of other uncontrollable events. It becomes a bug when one or more of the possible behaviors is undesirable.

StackOverFlow: A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don’t know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are “racing” to access/change the data.

參考文件