Created
September 6, 2015 07:31
-
-
Save muralikrishnat/722f4bbfdc6a79617930 to your computer and use it in GitHub Desktop.
Asynchronous Javascript Execution
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
//core method to handle execution | |
//has two arguments one is Array of methods that has to execute | |
//second param is enabling step by step execution. | |
function makeexecution(methodsArray, isStepbystep) { | |
//creating promise Object to return | |
var promise = {}; | |
//method to execute steps one by one If isStepbystep flag is set as true | |
//It uses to execute array of methods in synchronous manner. | |
function executeNextMethod(methodsArray, methodIndex, callback) { | |
if (methodsArray.length === methodIndex) { | |
callback(); | |
} else { | |
methodsArray[methodIndex](function (fnCompleted) { | |
methodIndex = methodIndex + 1; | |
console.log(fnCompleted); | |
//this recurrsive callin gis meant for step by step execution | |
//after completion of first step we should invoke next execution step | |
//so we will pass array of methods to execute and index of method that is next to current completed step | |
//and callback which is final call for all execution steps | |
executeNextMethod(methodsArray, methodIndex, callback) | |
}); | |
} | |
} | |
//this is promise method that can return to actual promise | |
promise.then = function (callback) { | |
var methodCount = methodsArray.length; | |
if (isStepbystep) { | |
executeNextMethod(methodsArray, 0, callback); | |
} else { | |
for (methodIndex = 0; methodIndex < methodCount; methodIndex++) { | |
methodsArray[methodIndex](function (fnCompleted) { | |
methodCount = methodCount - 1; | |
console.log(fnCompleted); | |
if (methodCount === 0) { | |
callback(); | |
} | |
}); | |
} | |
} | |
}; | |
return promise; | |
} | |
//execution method 1 | |
function f1(stepExecution) { | |
setTimeout(function () { | |
stepExecution('f1 method completed'); | |
}, 2000); | |
} | |
//execution method 2 | |
//will invoke after 300 millisecnds | |
function f2(stepExecution) { | |
setTimeout(function () { | |
stepExecution('f2 method completed'); | |
}, 300); | |
} | |
function f3(){ | |
console.log('all methods are done'); | |
} | |
// Usage : | |
//pass array of methods to execute | |
//out put : all methods are done.. will display after completion of f1 and f2 methods execution | |
//order of completion f2 will complete frst as we set timeout less than f1 | |
//f1 will complete last and will invoke final callback | |
//order of execution f2 -> f1 -> f3 (as per this example it may change based on completion of array of methods...) | |
makeexecution([f1,f2]).then(f3); | |
//setting istepbystep flag to true means... | |
//f2 will invoke after completion of f1 | |
//execution order is same as array order f1 -> f2 -> f3 | |
makeexecution([f1,f2],true).then(f3); | |
//Note : the above snipplet is meant to understand how promise will return and | |
//how to controll the execution of asynchronous execution | |
//it can be achieved by using come existing features in so many libraries like defferd...etc., | |
//f1 and f2 are methods which will run asynchronous calls/ansynchronous execution /sleep or timeout executions | |
//code can be change to fullfil more features like handling results of each step... | |
//Thanks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment