Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript';螺纹连接';图书馆?_Javascript_Multithreading_Dom Events - Fatal编程技术网

Javascript';螺纹连接';图书馆?

Javascript';螺纹连接';图书馆?,javascript,multithreading,dom-events,Javascript,Multithreading,Dom Events,我想执行一些操作,这些操作将异步执行(通过“完成”回调将数据发送到服务器)。然后,我想在所有这些任务完成后触发一个事件。类似于没有线程的线程连接。我编写了以下helper函数,它实现了我想要的功能: // A quick fake for thread joining. // Allow objects to add and remove themselves. Once they are all removed, call the callback with the context. fun

我想执行一些操作,这些操作将异步执行(通过“完成”回调将数据发送到服务器)。然后,我想在所有这些任务完成后触发一个事件。类似于没有线程的线程连接。我编写了以下helper函数,它实现了我想要的功能:

// 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不是多线程的。)

jQuery Deferred/Promise方法就是这样做的


jQuery Deferred/Promise方法正是这样做的