How to wait for time interval?

I am busy with a node.js project communicating with an API which involves heavy use of a node library specific to that API. I have read (I think) all the existing questions about the kind of issues involved with pausing and their various solutions but still not sure how to apply a correct solution to my problem.

Simply put, I have a function I call multiple times from the API library and need to ensure they have all completed before continuing. Up to now I have managed to use the excellent caolan/async library to handle my sync/async needs but hit a block with this specific function from the API library.

The function is hideously complicated as it involves https and SOAP calling/parsing so I am trying to avoid re-writing it to behave with caolan/async, in fact I am not even sure at this stage why it is not well behaved.

It is an async function that I need to call multiple times and then wait until all the calls have completed. I have tried numerous ways of of using callbacks and even promises (q library) but just cannot get it to work as expected and as I have successfully done with the other async API functions.

Out of desperation I am hoping for a kludgy solution where I can just wait for say 5 seconds at a point in my program while all existing async functions complete but no further progress is made until 5 seconds have passed. So I want a non-blocking pause of 5 seconds if that is even possible.

I could probably do this using fibres but really hoping for another solution before I go down that route.

one simple solution to your problem would be to increment a counter every time you call your function. Then at the end of the callback have it emit an event. listen to that event and each time it's triggered increment a separate counter. When the two counters are equal you can move on.

This would look something like this

var function_call_counter = 0;
var function_complete_counter = 0;
var self = this;

for(var i = 0; i < time_to_call; i++){

    function_call_counter++;

    api_call(function(){
        self.emit('api_called');        
    });
}

this.on('api_called', function(){
    function_complete_counter++;
});

var id = setInterval(function(){
    if(function_call_counter == function_complete_counter){
        move_on();
        clearInterval(id); // This stops the checking
    }
}, 5000 ); // every 5 sec check to see if you can move on

Promises should work also they just might take some finessing. You mentioned q but you may want to check out promises A+