I want to implement an asynchronous task scheduling. This is my code:
let gameTick = 0;
const tasks: Record<string, Function[]> = {};
function sleep(t: number) {
const targetTick = gameTick + t;
if (!tasks[targetTick]) {
tasks[targetTick] = [];
}
return new Promise<void>(resolve => {
tasks[targetTick].push(resolve);
});
}
async function task1() {
print('start task1');
for (let i = 0; i < 3; i++) {
print(`execute: ${i}`);
await sleep(2);
}
}
// task1();
for (gameTick = 0; gameTick < 10; gameTick++) {
print(`tick: ${gameTick}`);
tasks[gameTick]?.forEach(f => f())
if (gameTick == 2) task1();
}
This code behaves differently in the TypeScriptToLua environment and in the node.js environment, which confuses me.
TypeScriptToLua | nodejs |
---|---|
tick: 0 | tick: 0 |
tick: 1 | tick: 1 |
tick: 2 | tick: 2 |
start task1 | start task1 |
execute: 0 | execute: 0 |
tick: 3 | tick: 3 |
tick: 4 | tick: 4 |
execute: 1 | tick: 5 |
tick: 5 | tick: 6 |
tick: 6 | tick: 7 |
execute: 2 | tick: 8 |
tick: 7 | tick: 9 |
tick: 8 | execute: 1 |
tick: 9 |
I can understand the process of TypeScriptToLua, which is equivalent to resuming the coroutine after resolve and immediately executing the code after await. But I don’t understand NodeJs. Why does it continue to execute after the loop ends, and only execute once?