2 title: require-atomic-updates
8 When writing asynchronous code, it is possible to create subtle race condition bugs. Consider the following example:
13 async function addLengthOfSinglePage(pageNum) {
14 totalLength += await getPageLength(pageNum);
17 Promise.all([addLengthOfSinglePage(1), addLengthOfSinglePage(2)]).then(() => {
18 console.log('The combined length of both pages is', totalLength);
22 This code looks like it will sum the results of calling `getPageLength(1)` and `getPageLength(2)`, but in reality the final value of `totalLength` will only be the length of one of the two pages. The bug is in the statement `totalLength += await getPageLength(pageNum);`. This statement first reads an initial value of `totalLength`, then calls `getPageLength(pageNum)` and waits for that Promise to fulfill. Finally, it sets the value of `totalLength` to the sum of `await getPageLength(pageNum)` and the *initial* value of `totalLength`. If the `totalLength` variable is updated in a separate function call during the time that the `getPageLength(pageNum)` Promise is pending, that update will be lost because the new value is overwritten without being read.
24 One way to fix this issue would be to ensure that `totalLength` is read at the same time as it's updated, like this:
27 async function addLengthOfSinglePage(pageNum) {
28 const lengthOfThisPage = await getPageLength(pageNum);
30 totalLength += lengthOfThisPage;
34 Another solution would be to avoid using a mutable variable reference at all:
37 Promise.all([getPageLength(1), getPageLength(2)]).then(pageLengths => {
38 const totalLength = pageLengths.reduce((accumulator, length) => accumulator + length, 0);
40 console.log('The combined length of both pages is', totalLength);
46 This rule aims to report assignments to variables or properties in cases where the assignments may be based on outdated values.
50 This rule reports an assignment to a variable when it detects the following execution flow in a generator or async function:
52 1. The variable is read.
53 2. A `yield` or `await` pauses the function.
54 3. After the function is resumed, a value is assigned to the variable from step 1.
56 The assignment in step 3 is reported because it may be incorrectly resolved because the value of the variable from step 1 may have changed between steps 2 and 3. In particular, if the variable can be accessed from other execution contexts (for example, if it is not a local variable and therefore other functions can change it), the value of the variable may have changed elsewhere while the function was paused in step 2.
58 Note that the rule does not report the assignment in step 3 in any of the following cases:
60 * If the variable is read again between steps 2 and 3.
61 * If the variable cannot be accessed while the function is paused (for example, if it's a local variable).
63 Examples of **incorrect** code for this rule:
68 /* eslint require-atomic-updates: error */
72 async function foo() {
73 result += await something;
76 async function bar() {
77 result = result + await something;
80 async function baz() {
81 result = result + doSomething(await somethingElse);
84 async function qux() {
86 result = await initialize();
90 function* generator() {
97 Examples of **correct** code for this rule:
102 /* eslint require-atomic-updates: error */
106 async function foobar() {
107 result = await something + result;
110 async function baz() {
111 const tmp = doSomething(await somethingElse);
115 async function qux() {
117 const tmp = await initialize();
124 async function quux() {
125 let localVariable = 0;
126 localVariable += await something;
129 function* generator() {
130 result = (yield) + result;
138 This rule reports an assignment to a property through a variable when it detects the following execution flow in a generator or async function:
140 1. The variable or object property is read.
141 2. A `yield` or `await` pauses the function.
142 3. After the function is resumed, a value is assigned to a property.
144 This logic is similar to the logic for variables, but stricter because the property in step 3 doesn't have to be the same as the property in step 1. It is assumed that the flow depends on the state of the object as a whole.
146 Example of **incorrect** code for this rule:
151 /* eslint require-atomic-updates: error */
153 async function foo(obj) {
155 obj.something = await getSomething();
162 Example of **correct** code for this rule:
167 /* eslint require-atomic-updates: error */
169 async function foo(obj) {
171 const tmp = await getSomething();
183 This rule has an object option:
185 * `"allowProperties"`: When set to `true`, the rule does not report assignments to properties. Default is `false`.
189 Example of **correct** code for this rule with the `{ "allowProperties": true }` option:
194 /* eslint require-atomic-updates: ["error", { "allowProperties": true }] */
196 async function foo(obj) {
198 obj.something = await getSomething();
205 ## When Not To Use It
207 If you don't use async or generator functions, you don't need to enable this rule.