running code when two events have triggered

Posted by Evert on Stack Overflow See other posts from Stack Overflow or by Evert
Published on 2011-03-14T23:44:55Z Indexed on 2011/03/15 0:10 UTC
Read the original article Hit count: 176

This is mostly a language-agnostic question.

If I'm waiting for two events to complete (say, two IO events or http requests), what is the best pattern to deal with this. One thing I can think of is the following (pseudo js example).

request1.onComplete = function() {
  req1Completed = true;
  eventsCompleted();
}

request2.onComplete = function() {
  req2Completed = true;
  eventsCompleted();
}

eventsCompleted = function() {

  if (!req1Completed || !req2Completed) return;
  // do stuff

}

Is this the most effective pattern, or are there more elegant ways to solve this issue?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about events