2011-10-12 2 views
1

비동기로 실행되는 여러 작업을 수행하려고합니다 ('완료'콜백을 사용하여 서버에 데이터 보내기). 그런 다음 모든 작업이 완료되면 이벤트를 트리거하려고합니다. 스레드가없는 스레드 조인과 같은 것입니다.Javascript 'thread join'library?

// 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되지 않았 음을 알 수 있습니다.)

답변