how to sync between two functions
javascript
Solution
If 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?
Problem
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 ?