2011-10-12 25 views
1

我想要執行一些操作,它們將異步執行(通過'完成'回調將數據發送到服務器)。然後我想在所有這些任務完成時觸發一個事件。像線程一樣沒有線程連接。我寫了以下幫助函數,它可以做我想做的事情:Javascript'線程連接'庫?

// A quick fake for thread joining. 
// Allow objects to add and remove themselves. Once they are all removed, call the callback with the context. 
function Waiter(callback, context) 
{ 
    this.tarriers = []; 
    this.callback = callback; 
    this.context = context; 

    this.add = function(tarrier) 
    { 
     this.tarriers.push(tarrier); 
    } 

    this.remove = function(tarrier) 
    { 
     this.tarriers = _.without(this.tarriers, tarrier); 

     if (this.tarriers.length == 0) 
     { 
      this.callback(this.context); 
     } 
    } 

    return this; 
} 

但是我覺得輪子重新發明有點不好。有沒有一個我可以使用的庫,可以爲我做這種事情(可能還有其他相關的事情)。

(我知道JS不mulithreaded。)

回答