开发者

Execute non-blocking task after blocking tasks

In my application I have n blocking tasks, once all blocking tasks are done executing, I want to finally execute my non-blocking task (which relies on data from these blocking tasks).

My current solution is to have a variable i getting incremented by one in the blocking callback, afterwards it checks if i is n, if so, it executes the dependent, non-blocking task.

However, I feel this is nowhere near perfect; I assume it's a fundamental issue with async programming with a good solution (although I've been unable to find a better one).

var i;

var result_one = null;
var result_two = null;

function render() {
    if (i == 2) {
     开发者_C百科   // do something with result_one and result_two
    }
}

blocking.call(function(result) {
    result_one = result;
    i++;
    render();
});

blocking.call2(function(result) {
    result_two = result;
    i++;
    render();
});


https://github.com/creationix/step

Step(
  // Loads two files in parallel
  function loadStuff() {
    fs.readFile(__filename, this.parallel());
    fs.readFile("/etc/passwd", this.parallel());
  },
  // Show the result when done
  function showStuff(err, code, users) {
    if (err) throw err;
    sys.puts(code);
    sys.puts(users);
  }
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜