Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let locked = false
- const checkCanCall = (resolve) => {
- if (locked) {
- return setTimeout(() => checkCanCall(resolve), 100)
- }
- locked = true
- resolve()
- };
- const waitUntilFree = () => new Promise(checkCanCall)
- const unlock = () => locked = false
- const synchronize = (action) => {
- return waitUntilFree()
- .then(() => action())
- .finally(unlock)
- };
- synchronize(() => {
- return new Promise((r) =>{
- setTimeout(r, 3000);
- }).then(r => {
- console.info('First Promise, after 3s')
- })
- }).then(()=>{
- console.info('After first promise')
- })
- synchronize(() => {
- return new Promise((r) =>{
- setTimeout(r, 3000);
- }).then(r => {
- console.info('Second Promise, after 6s')
- })
- }).then(()=>{
- console.info('After second promise')
- })
- console.info('Immediately executed, wait for 3s')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement