Skip to content

Instantly share code, notes, and snippets.

@Developer-Amit
Created October 24, 2020 13:45
Show Gist options
  • Save Developer-Amit/86a92c969fd3dfc7ce6084852fef8229 to your computer and use it in GitHub Desktop.
Save Developer-Amit/86a92c969fd3dfc7ce6084852fef8229 to your computer and use it in GitHub Desktop.
Async Tasks running
function Runner() {
// Write your code here
// Amit's code start
function runFunctions() {
// Getting arguments from prototype
var tasks = Array.prototype.concat.apply([], arguments);
//console.log(tasks, "functions lists");
// Shifing tasks functions into a que
var task = tasks.shift();
// Executing one by one
task(function() {
if(tasks.length > 0)
// if there are functions exist in tasks list make call and callback one by one
//console.log(tasks, "Final in");
runFunctions.apply(this, tasks);
});
}
// Final call of functions
runFunctions(f1, f2, f3, f4);
// Amit's code end
}
function f1(done) {
setTimeout(function() {
console.log('Executing f1');
done();
}, 100);
}
function f2(done) {
setTimeout(function() {
console.log('Executing f2');
done();
}, 200);
}
function f3(done) {
setTimeout(function() {
console.log('Executing f3');
done();
}, 20);
}
function f4(done) {
setTimeout(function() {
console.log('Executing f4');
done();
}, 10);
}
var runner = new Runner();
runner.run(f1);
runner.run(f2);
runner.run(f3);
runner.run(f4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment