Javascript';螺纹连接';图书馆?
我想执行一些操作,这些操作将异步执行(通过“完成”回调将数据发送到服务器)。然后,我想在所有这些任务完成后触发一个事件。类似于没有线程的线程连接。我编写了以下helper函数,它实现了我想要的功能: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
// 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方法正是这样做的