mirror of
https://github.com/bendtherules/JSQuestions.git
synced 2026-08-19 00:30:48 +00:00
async scheduler 1
This commit is contained in:
@@ -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');
|
||||
})();
|
||||
Reference in New Issue
Block a user