Created
April 29, 2022 16:05
-
-
Save faustienf/537712cf741412eb2aafecefb9df307a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createQueue = () => { | |
let queue = Promise.resolve(); | |
return <T extends () => any>(task: T) => { | |
queue = queue.then(() => task()); | |
}; | |
}; | |
const queue = createQueue(); | |
// example | |
const delay = (ms: number) => { | |
new Promise((resolve) => { | |
setTimeout(resolve, ms); | |
}); | |
}; | |
queue(() => delay(1000).then(() => console.log('1'))); | |
queue(() => delay(100).then(() => console.log('2'))); | |
queue(() => console.log('3')); | |
// output: 1, 2, 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment