I have a function called parsedata in my node.js file which is called when a user logs in. After parsedata() is called, the server switches to a new screen. However, this only works every other time. I put an asynchronous wait in between, which made it work about 90% of the time but I am just wondering why it is doing this. I believe it has something to do with all of the helper functions which are being used but I am not completely sure. Any info or help would be greatly appreciated!
app.post("/login.html", urlencodedParser, async (req, res) => {
await parseData();
//await sleep(750);
res.redirect(__dirname "/homescreen.html");
});
async function parseData() {
let dates = await findCommon();
let maxStreak = await getMaxStreak(dates);
}
async function findCommon() {
var dates = new Set();
var data = await fs.readFile(__dirname "/mem.txt", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
return data;
});
for (let i = 0; i < data.length; i ) {
if (data[i] === "*" && i mostRecentName.length < data.length) {
if (data.slice(i 1, i mostRecentName.length 1) == mostRecentName) {
while (data[i] != "\n") {
i ;
}
if (i < data.length - 1) {
i ;
}
while (data[i] != "*" && i < data.length) {
let curr = "";
let count = 10;
while (count > 0) {
count--;
curr = data[i];
i ;
}
while (data[i] != "\n") {
i = 1;
}
if (i < data.length - 1) {
i ;
}
dates.add(curr);
}
}
}
}
dates = Array.from(dates);
dates = await bubbleSort(dates);
return dates;
}
async function getMaxStreak(dates) {
let today = new Date();
let year = today.getFullYear().toString();
let month = (today.getMonth() 1).toString();
let day = today.getDate().toString();
if (month.length == 1) {
month = "0" month;
}
if (day.length == 1) {
day = "0" day;
}
let testDate = year "-" month "-" day;
if (!(testDate in dates)) {
dates.push(testDate);
}
let streak = 1;
for (let i = dates.length - 1; i > 0; i--) {
let options;
if (i == dates.length - 1) {
options = await convert(testDate);
} else {
options = await convert(dates[i]);
}
if (dates[i - 1] == options[0] || dates[i - 1] == options[1] || dates[i - 1] == options[2]) {
streak ;
} else {
return streak;
}
}
return streak;
}
async function convert(date) {
let option1Day = (parseInt(date.slice(8, 10)) - 1).toString();
if (option1Day.length == 1) {
option1Day = "0" option1Day;
}
let option2Month = (parseInt(date.slice(5, 7)) - 1).toString();
if (option2Month.length == 1) {
option2Month = "0" option2Month;
}
let option2Day = "30";
let option3Day = "31";
let option1 = date.slice(0, 8) option1Day;
let option2 = date.slice(0, 5) option2Month "-" option2Day;
let option3 = date.slice(0, 5) option2Month "-" option3Day;
return [option1, option2, option3];
}
CodePudding user response:
It has something with the macro and micro tasks.Your code has the same result with the following codes:
new Promise((resolve, reject) => {
findCommon().then(dates => {
getMaxStreak(dates).then(maxStreak => {})
})
resolve()
})
.then(() => {
res.redirect(__dirname "/homescreen.html")
})
res.redirect
will be added into the micro task queue;
then, getMaxStreak
will be added into the micro task queue too.
finally, we will take out the first task of the micro task queue to execute, yes, it's res.redirect
, not getMaxStreak
.