I have two functions that I want to call to third function when the other functions( one and two ) will be finished. I need that the first function and the second function will be called Asynchronous. for example
var func1 = function( do something..... return arr )
var func2 = function ( do something ..... return arr2 )
if ( arr.length > 0 && arr2.length > 0 )
var func3 = function( do something )
my qeustions:
what is the best way to do it ?
How I call to function in Asynchronous way ?
I have two functions that I want to call to third function when the other functions( one and two ) will be finished. I need that the first function and the second function will be called Asynchronous. for example
var func1 = function( do something..... return arr )
var func2 = function ( do something ..... return arr2 )
if ( arr.length > 0 && arr2.length > 0 )
var func3 = function( do something )
my qeustions:
what is the best way to do it ?
How I call to function in Asynchronous way ?
- So you want to call the third function after you execute the first and the second? – Vlad Balmos Commented Aug 23, 2012 at 13:35
- yes but I want that the first function and the second function will be called in Asynchronous way – user1365697 Commented Aug 23, 2012 at 13:39
- what do you mean by "Asynchronous way"? – Vlad Balmos Commented Aug 23, 2012 at 13:40
- I mean that I call to func1 and I don't know when the function will be finsihed , also it is possible that the func2 will finish before the first function.Only when I have the data of arr and arr2 I need to call to func3 – user1365697 Commented Aug 23, 2012 at 13:41
- I don't want to call synchronous way meaning to call func1 get result and then to call to func2 and get result.I don't know when the function will be finished – user1365697 Commented Aug 23, 2012 at 13:43
4 Answers
Reset to default 5If you have jQuery, you could use their Deferred
objects:
var func1 = function () {
var dfd = $.Deferred();
setTimeout(function () {
// do your processing
dfd.resolve(arr1);
}, 0);
return dfd.promise();
};
var func2 = function () {
var dfd = $.Deferred();
setTimeout(function () {
// do your processing
dfd.resolve(arr2);
}, 0);
return dfd.promise();
};
$.when(func1(), func2()).then(function (arr1, arr2) {
if ( arr.length > 0 && arr2.length > 0 ) {
func3();
}
});
Related questions:
- How can jQuery deferred be used?
- How can I create an Asynchronous function in Javascript?
Pass the func3 function as a callback and check inside your callback if both arrays are filled.
var func1 = function(callback) ( do something; callback();..... return arr );
var func2 = function(callback) ( do something; callback(); ..... return arr2 );
var func3 = function() {
if(arr != undefined && arr2 != undefined){
//do stuff
}
};
func1(func3);
func2(func3);
var arr1 = [], arr2 = [];
function arraysAreReady() {
if(arr1.length && arr2.length) {
func3();
}
}
var func1 = function() {
var localArray = [];
/// create the local array
arr1 = localArray;
arraysAreReady();
}
var func2 = function() {
var localArray = [];
/// create the local array
arr2 = localArray;
arraysAreReady();
}
func1(); funct2();
when func1 has created the first array it wil assign it to the global variable arr1 and call arraysAreReady() function. when func2 has created the second array it will asign it to the global variable arr2 and call arraysAreReady().
the arraysAreReady() function checks on each call if the 2 global arrays are not empty. Is they are not the it will call the third function (func3)
If your program logic is suitable, you can imitate asynchronous behavior in javascript by doing partial processing and using setTimeout() function as follows:
var result1 = []
var result1Ready = false;
var result2 = []
var result2Ready = false;
func1 = function(list, start, end) {
if(start>=end) {
result1Ready = true;
}
else {
partialEnd = start+10>end ? end : start+10;
for(i=start;i<partialEnd;i++) {
//process 10 items
//append results to array result1
}
//schedule second partial process
setTimeout(function () { func1(list, partialEnd, end); }, 50);
}
};
func2 = function(list, start, end) {
//similar to func1...
};
waitResults = function() {
if(result1Ready && result2Ready) {
func3();
}
else {
setTimeout(function () { waitResults(); }, 50);
}
};
setTimeout(function () { func1(someList, 0, listLength); }, 5);
setTimeout(function () { func2(someOtherList, 0, otherListLength); }, 5);
setTimeout(function () { waitResults(); }, 10);