Comprehending Async.runSync Callback


#1

I am trying to use Async.runSync. The example in the documentation is… terse. Since waiting for a given time, why use the function at all in such a scenario other than for an example.

What I am trying to do is set the value of a variable to the return result of a method within the parent object, the sibling method being called is an external API call. The very next statement in the method requires the returned value.

Currently, the sibling returns an array. The broken code that I want to work is as follows:

var array = this.getArrayFromAPI();
var string = array[array.length - 1];

I do not want to create a huge callback chain at this point in the code. Therefore, I am thinking runAsync should work but I do not quite understand how to trigger it properly. I modified myFriends to take a function for a parameter, and return it with:

return callback (error, array);

My theory is:

var array = Async.runSync (this.getArrayFromAPI(done));

or perhaps:

var array = Async.runSync ( function (done) {
    return self.getArrayFromAPI (done);
});

I don’t usually use forums, but … saw the tweet and thought I’d check it out…and just happened to have a problem at the time! :smile:

Thanks in advance,
iDM