async scheduler 1

This commit is contained in:
2026-02-06 13:52:15 +05:30
parent eb9af2f6ae
commit 95ce34c83d
5 changed files with 470 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
const TaskScheduler = require('./async-scheduler');
function wait(ms, label) {
return () => new Promise((resolve) => {
console.log(`${label} started`);
setTimeout(() => {
console.log(`${label} finished`);
resolve(label);
}, ms);
});
}
(async () => {
const scheduler = new TaskScheduler(1);
scheduler.play();
const p1 = scheduler.addTask(wait(200, 'A'));
const p2 = scheduler.addTask(wait(50, 'B'));
// Pause immediately; B should not start until play()
scheduler.pause();
await p1;
console.log('p1 done');
// Wait 100ms to see if B started (it shouldn't)
await new Promise(r => setTimeout(r, 100));
console.log('After wait, now play again');
scheduler.play();
await p2;
console.log('p2 done');
})();