mirror of
https://github.com/bendtherules/JSQuestions.git
synced 2026-08-18 13:53:51 +00:00
32 lines
761 B
JavaScript
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');
|
|
})(); |