notification
might be reassigned based on an outdated value of notification
460 } else {
461 if(cansendNoti) {
462 let notification = new Notification('IPost', { body: "new message posted from " + user , tag: "new_post"});
463 notification = await notification464 notification.addEventListener("click",function(){
465 notification.close()
466 })
user_cache[username]
might be assigned based on an outdated state of user_cache
132 } else {
133 user = "/images/default_avatar.png"
134 }
135 user_cache[username]=user136 }
137 return user
138}
notification
might be reassigned based on an outdated value of notification
368 } else {
369 if(cansendNoti) {
370 let notification = new Notification('IPost', { body: "new dm from " + user , tag: "new_post"});
371 notification = await notification372 notification.addEventListener("click",function(){
373 notification.close()
374 })
When writing asynchronous code, it is possible to create subtle race condition bugs. Consider the following example:
let totalLength = 0;
async function addLengthOfSinglePage(pageNum) {
totalLength += await getPageLength(pageNum);
}
Promise.all([addLengthOfSinglePage(1), addLengthOfSinglePage(2)]).then(() => {
console.log('The combined length of both pages is', totalLength);
});
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.
One way to fix this issue would be to ensure that totalLength
is read at the same time as it's updated, like this:
async function addLengthOfSinglePage(pageNum) {
const lengthOfThisPage = await getPageLength(pageNum);
totalLength += lengthOfThisPage;
}
Another solution would be to avoid using a mutable variable reference at all:
Promise.all([getPageLength(1), getPageLength(2)]).then(pageLengths => {
const totalLength = pageLengths.reduce((accumulator, length) => accumulator + length, 0);
console.log('The combined length of both pages is', totalLength);
});
let result;
async function foo() {
result += await somethingElse;
result = result + await somethingElse;
result = result + doSomething(await somethingElse);
}
function* bar() {
result += yield;
result = result + (yield somethingElse);
result = result + doSomething(yield somethingElse);
}
let result;
async function foo() {
result = await somethingElse + result;
const tmp = await somethingElse;
result += tmp;
const localVariable = 0;
localVariable += await somethingElse;
}
function* bar() {
result += yield;
result = (yield somethingElse) + result;
result = doSomething(yield somethingElse, result);
}