3

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:

  1. what is the best way to do it ?

  2. How I call to function in Asynchronous way ?

6
  • So you want to call the third function after you execute the first and the second? 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 Commented Aug 23, 2012 at 13:39
  • what do you mean by "Asynchronous way"? 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 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 Commented Aug 23, 2012 at 13:43

4 Answers 4

5

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, Deferred & Promise is pretty neat!
0

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);

Comments

0
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)

2 Comments

but the problem that I dont know when the func1 and func2 will be finished , I need that those function will be called Asynchronous so I don't know when arr and arr2 have a data
I can use the func3 only when both function will finished
0

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.