admin管理员组文章数量:1356595
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;
}
};
本文标签:
版权声明:本文标题:javascript - ESLint: Possible race condition: `user.registered` might be reassigned based on an outdated value of `user.register 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744065118a2584846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论