Created
October 30, 2016 07:27
-
-
Save hasangilak/ab02d0e09178ae59f1d13b32df91496f to your computer and use it in GitHub Desktop.
I want one master action to complete its job asynchronously and at the end of its job free its slaves so I wrote this tiny library.
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
let master = () => { | |
return new Promise( (resolve, reject) => { | |
setTimeout(() => { resolve('ok'); }, 3000); | |
}); | |
} | |
let slaves = [ | |
{ | |
start: () => { | |
console.log("loading") | |
}, | |
end: () => { | |
console.log("loading finished") | |
} | |
}, | |
{ | |
start:() => { | |
console.log("other job") | |
}, | |
end: () => { | |
console.log('end other job') | |
} | |
} | |
]; | |
let resolvers = []; | |
master().then( | |
response => { | |
resolvers.forEach(({resolve, end}) => {resolve(); end();}); | |
console.log("master is finished"); | |
}, | |
error => console.log("this code should not failed") | |
); | |
slaves.forEach(({start, end}) => { | |
new Promise( (resolve, reject) => { | |
start(); | |
resolvers.push({resolve, end}); | |
}) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment