0

I have two javascript functions.

This is one function and other one is also same.

function function1(.., .., callback){
    //database logic

 callback(data);

}

My requirement is to get data from both functions and send to client side.

function sendFunction(){

   function1(.., .., function(data1)){
      console.log(data1);
   }

   function2(.., .., function(data2)){
      console.log(data2);
   }

   sendToClient(data1+data2);
}

My issue is how can I take outside data1 and data2 from functions to concatenate. Guide me to achieve this task.

7
  • are you returning data1 and data2 from both the functions? Commented Sep 23, 2016 at 5:18
  • using callbacks for both functions Commented Sep 23, 2016 at 5:21
  • did you print console.log(data1, data2) ? Commented Sep 23, 2016 at 5:23
  • yes, console.log(data1) and console.log(data2) works fine. console.log(data1 + data2) returns empty. Commented Sep 23, 2016 at 5:25
  • returns empty means does it return undefined or anything else ? Commented Sep 23, 2016 at 5:27

3 Answers 3

2

Considering that function1 and function2 are synchronous, you have to create two variables outside the functions to store the returned values.

function sendFunction () {
    var data1, data2;

    function1(.., .., function(d1) {
        data1 = d1;
        console.log(data1);
    });

    function2(.., .., function (d2) {
        data2 = d2;
        console.log(data2);
    });

    sendToClient(data1 + data2);
}

EDIT:

For async functions, I suggest you to use Promises:

function function1 () {
    return new Promise(function (resolve, reject) {
        // logic here

        // If everything is ok, call resolve().
        resolve("data1");

        // If something went wrong, call reject().
    });
}

function function2 () {
    return new Promise(function (resolve, reject) {
        // logic here

        // If everything is ok, call resolve().
        resolve("data2");

        // If something went wrong, call reject().
    });
}

function sendFunction () {
    Promise.all([function1(), function2()])
    .then(function (result) {
        sendToClient(result[0] + result[1]);
    })
    .catch(function () {
        console.log("The request failed. Try again.")
    });
}

sendFunction();
Sign up to request clarification or add additional context in comments.

3 Comments

With promises, you should just use Promise.all. Outer-scope variables are a horrible practice.
@Bergi Fixed. I used to use jQuery's $.when(). I'm still getting used to JavaScript Promises. Thanks.
Thanks, +1 :-) The updated version also has the benefit of running the two requests in parallel.
0

Do sth like:

var datas=[];
var count=0;
function callback(data){
count++;
datas.push(data);
if(count==2){
//perfect both callbacks returned
proceedwith(datas);
}
}
function1(callback);
function2(callback);

If they are json objects, you cannot add them. Store them into an array, or inside of an object, or stringify them

1 Comment

In here I intepreted what I need to do. You are correct. I need array to store two objects. but that is not the issue in here
0

Finally, I found a way to do this.

function sendFunction () {      

    function1(.., .., function(data1) {
        console.log(data1);

        function2(.., .., function(data2) {
           console.log(data2);

            sendToClient(data1 + data2);
        });
    });


    sendToClient(data1 + data2);
}

2 Comments

This is not a good way because it includes nesting. If you have more than 2 functions, it becomes complicated. Please, check my updated answer using the new JavaScript Promises.
You don't need to use var data1, data2 and those assignments. Just go for function(data1) { and function(data2) { directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.