Files
JSQuestions/solutions/test-async-scheduler-pause.js
2026-02-06 13:52:15 +05:30

32 lines
761 B
JavaScript

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');
})();