是否有任何原子 javascript 操作来处理 Ajax 的异步性质?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7266918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are there any atomic javascript operations to deal with Ajax's asynchronous nature?
提问by puk
I am dynamically loading code (functions) from a server and executing it as javascript code then storing it in an array and executing. All these snippets of code must be executed exactly once. The psuedocode follows as such
我正在从服务器动态加载代码(函数)并将其作为 javascript 代码执行,然后将其存储在数组中并执行。所有这些代码片段必须只执行一次。伪代码如下
function fetch(foo){
if (foo in fooArray){
//Do Nothing
else{
//Fetch foo via Ajax and execute foo()
}
}
The problem is vastly more complex, but essentially if I issue the below command
问题要复杂得多,但基本上如果我发出以下命令
fetch('someFunctionName');
fetch('someFunctionName');
fetch('someFunctionName');
fetch('someFunctionName');
all four will execute the if (foo in fooArray)
and assume that it is not in the array, and all four will proceed to fetch the code and execute it. I remember back in the day learning about semaphores and mutexes, are there such things for javascript.
所有四个都将执行if (foo in fooArray)
并假设它不在数组中,并且所有四个将继续获取代码并执行它。我记得在那天学习信号量和互斥量时,javascript 是否有这样的东西。
回答by Tomasz Nurkiewicz
JavaScript is a nice language that works great with asynchronous callbacks, timeouts, intervals and user events, yet not having any concurrency problems. This is possible because JavaScript is essentially single-threaded - given piece of code is always executed atomically and never interrupted by another thread running JavaScript.
JavaScript 是一种很好的语言,可以很好地处理异步回调、超时、间隔和用户事件,但没有任何并发问题。这是可能的,因为 JavaScript 本质上是单线程的——给定的代码片段总是以原子方式执行,永远不会被另一个运行 JavaScript 的线程中断。
Your fetch()
function will always be executed without any interruption. If it is executed as part of the AJAX callback and if multiple AJAX callbacks are pending, they will be queued.
您的fetch()
功能将始终执行而不会中断。如果它作为 AJAX 回调的一部分执行并且多个 AJAX 回调未决,则它们将排队。
Another example: if you have an event handler assigned to an input element and you fire the event multiple times at once, event handlers won't be executed concurrently. Instead they will be queued and executed sequentially. This also applies to multiple events triggered by setTimeout()
/setInterval()
.
另一个示例:如果您将事件处理程序分配给输入元素,并且您一次触发该事件多次,则事件处理程序将不会同时执行。相反,它们将排队并按顺序执行。这也适用于由setTimeout()
/触发的多个事件setInterval()
。
As a side-note: this is one of the reasons why node.jsis so robust: it uses only single thread and never blocks on I/O but uses callbacks instead when data is ready/event occurs.
作为旁注:这是node.js如此健壮的原因之一:它只使用单线程并且从不阻塞 I/O,而是在数据准备好/事件发生时使用回调。
回答by Ustaman Sangat
Javascript is essentially single-threaded so you don't need a mutex. Your fetch could set up flags such that subsequent fetch calls could avoid making ajax calls e.g.:
Javascript 本质上是单线程的,因此您不需要互斥锁。您的 fetch 可以设置标志,以便后续的 fetch 调用可以避免进行 ajax 调用,例如:
var beingFetched = {};//map onflight -> callbacks
function fetch(foo){
if (foo in fooArray){
//Do Nothing
} else {
if (beingFetched.foo) { //note empty array is truthy
//register a callback
var callback = function(r){
//anything you need to do wit the return object r
//maybe even eval it.
};
//the callback would more likely be an argument to fetch itself
//or you could use a promise API instead so that you can at your will
//register multiple callbacks - for error, for success etc.
beingFetched.foo.push(callback);
} else {
beingFetched.foo = [];//truthy
//Fetch foo via Ajax and execute
$.ajax("getFoo/"+foo).done(function() {
_.each(beingFetched.foo, function(cb){
cb.apply(cb,arguments);
});
delete beingFetched.foo;
});
}
}
}