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 ?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With