I have created an async function as below:
let createUserWrapper = async function(user){
await Log.createUser(user);
if (!user.registered) {
user.registered = true;
//userToSave has been declared outside of this function's scope
userToSave = true;
}
};
The line user.registered = true;
seems to show the warning under the user
object. I am not understanding why this shows, since whatever happens above in the await, we pass objects by reference so we have the latest value of the registered
key when the code continues.
I have created an async function as below:
let createUserWrapper = async function(user){
await Log.createUser(user);
if (!user.registered) {
user.registered = true;
//userToSave has been declared outside of this function's scope
userToSave = true;
}
};
The line user.registered = true;
seems to show the warning under the user
object. I am not understanding why this shows, since whatever happens above in the await, we pass objects by reference so we have the latest value of the registered
key when the code continues.
- eslint/docs/rules/require-atomic-updates – Bhojendra Rauniyar Commented Jul 11, 2019 at 8:44
- @BhojendraRauniyar My code is not affected by the example, since I am awaiting first and then continuing with my code execution. – Ncifra Commented Jul 11, 2019 at 8:57
- @Ncifra the eslint example also awaits inside the function, but eslint warns you that createUserWrapper might be called twice with the same user in a non-awaited context. – hraban Commented Mar 17, 2023 at 7:47
- @hraban what error code is it? I don't remember having another warning, but maybe I had those rules disabled at the time. – Ncifra Commented May 18, 2023 at 16:47
2 Answers
Reset to default 4This seems to be a false positive. ESLint is assuming that the await Log.createUser(user);
line is manipulating the user
object data, so if we were to have in parallel another async function similar to the above, like:
let createUserWrapper2 = async function(user) {
await Log.createUser2(user);
if (!user.registered) {
user.registered = true;
//userToSave has been declared outside of this function's scope
userToSave = true;
}
};
and then execute:
Promise.all([createUserWrapper(user), createUserWrapper2(user)]).then(() => {
...
});
Considering the parallel execution in Promise.all()
, the value of user.registered
in each of the two functions, may not be the value from the latest value, but the old one from the "own" async function.
I still think that it is a bit messy, and can't quite point out what it is supposed to show, considering that the execution is linear, and the objects are passed by reference, so each execution tick will have the latest object data.
Try this:
user.registered = false;
let createUserWrapper = async function(user){
await Log.createUser(user);
if (!user.registered) { // registered value from createUser(user)
user.registered = true;
//userToSave has been declared outside of this function's scope
userToSave = true;
}
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744065119a4552502.html
评论列表(0条)