0

I have a scenario like this:

function sendRequest(args){
   //some stuff here
   request = buildRequest();
   return request;
}

function buildRequest(){
   //some stuff here
   asyncCall1(args,function(err,result){
      //some stuff here
        asyncCall2(args2, function(err2,result2){
           //some stuff here
           //final request that is build is here inside async2
           return request;//I dont know if this is the correct way
       });
   });
}  

I need the request parameter which is available inside the asynCall2's callback method back to the sendRequest method so that I can return it form there.
I am very new to javascript as well as callbacks..all I need is to stop the execution of sendRequest method till I get the request parameter back from the asyncCalls (also how will I return that request parameter from asyncCall2).
The only constraint is I cannot change the signatures of sendRequest, asyncCall1 and asyncCall2 methods.

newbie
  • 663
  • 2
  • 6
  • 19
  • That's simply not possible (and doesn't make much sense if you'll take a moment to think about it). You're either synchronous, or you're asynchronous. You can't be both at the same time. – Amit Aug 31 '15 at 20:27
  • Can you be a little bit more comprehensive.. I am really new to this concept. I think i got your point but can you make this workflow correct? – newbie Aug 31 '15 at 20:29
  • Read up on the duplicate link. – Amit Aug 31 '15 at 20:31
  • @Amit Is it because I cant modify the signature of sendRequest method to support a callback? – newbie Aug 31 '15 at 20:32
  • I don't know anything about your sendRequest method. It looks like it is your code, so you can do whatever you want with it, but you'd really do yourself a big favor if you read the long and detailed answer in the duplicate link. seriously. – Amit Aug 31 '15 at 20:33
  • @Amit I am implementing a web service compliant to the standards of the Quickbooks webconnector where the signature of the sendRequest method is defined, So I cant really do anything about it. Its just that in my case, to send a request via the sendRequest method(as I dont have any choice there) I had to make some async calls to another server instance to fetch some data to prepare the request itself..Btw I read the duplicate link and it is quite enlightening..Thanks!..So i guess there is no work around for this then! – newbie Aug 31 '15 at 20:38

0 Answers0