简体   繁体   English

异步/同步 Javascript

[英]Asynchronous/Synchronous Javascript

I'm having some trouble understanding the difference between asynchronous and synchronous Javascript, and was hoping someone could shed some light on this.我在理解异步和同步 Javascript 之间的区别时遇到了一些麻烦,希望有人能对此有所了解。

I know Javascript is inherently synchronous, but you can use asynchronous events/callbacks to alter your program flow.我知道 Javascript 本质上是同步的,但是您可以使用异步事件/回调来改变您的程序流程。 However, what happens if you call a function with no callback that contains AJAX?但是,如果调用 function 而没有包含 AJAX 的回调,会发生什么?

For example, if I have the following code, where foo() contains some sort of server query and foobar() contains some output text:例如,如果我有以下代码,其中foo()包含某种服务器查询,而foobar()包含一些 output 文本:

foo();
foobar();

Will foobar() be called before the internal logic in foo() is complete, or will the browser wait until foo() is fully executed before calling foobar() ?是在foo()中的内部逻辑完成之前调用foobar() () ,还是浏览器会等到foo()完全执行后再调用foobar() (This seems simple, but my confusion arises from callbacks and whether or not they are absolutely necessary in all cases to control your program flow, ie if foo(foobar) is always necessary.) (这看起来很简单,但我的困惑来自回调,以及它们是否在所有情况下都是绝对必要的,以控制您的程序流程,即是否总是需要foo(foobar) 。)

Also, if foo() contains a server call that is quickly executed on the client side but takes a long time on the server to process, is a callback the only way I can make my program wait until foo() is completely done executing?此外,如果foo()包含一个在客户端快速执行但在服务器上需要很长时间来处理的服务器调用,那么回调是我可以让我的程序等到foo()完全执行完成的唯一方法吗?

foobar() will indeed be called before the Ajax call in foo() is complete... foobar() 确实会在 foo() 中的 Ajax 调用完成之前被调用...

Unless, and this is the answer to your second question, you specify that the Ajax call should be synchronous, which is an option.除非这是第二个问题的答案,否则您指定 Ajax 调用应该是同步的,这是一个选项。 Doing so will force the user to wait until the call completes before they can do anything, so that's usually not the best choice.这样做会迫使用户等到调用完成后才能执行任何操作,因此这通常不是最佳选择。 Using a callback is usually the best approach.使用回调通常是最好的方法。

Will foobar() be called before the internal logic in foo() is complete是否会在 foo() 中的内部逻辑完成之前调用 foobar()

The answer to this question depends on what you mean by "internal logic".这个问题的答案取决于你所说的“内部逻辑”是什么意思。 foobar will only be called once all of the javascript in foo is complete. foobar 只有foo中的所有foobar完成后才会被调用。 It will not however wait for the AJAX call to return (kind of the whole point of the A in AJAX).但是,它不会等待 AJAX 调用返回(AJAX 中 A 的全部点)。

All of foo will run, then all of foobar will run.所有的foo都会运行,然后所有的foobar都会运行。

Part of foo might say "Send an HTTP request asynchronously", in which case it won't wait for the response to come back before continuing (but since foo only triggers the sending of the request, it is considered complete). foo的一部分可能会说“异步发送 HTTP 请求”,在这种情况下,它不会等待响应返回再继续(但由于foo仅触发请求的发送,因此它被视为完成)。

If there was a callback, then that would run when the response arrived (which would usually be after foobar was complete).如果有回调,那么它将在响应到达时运行(通常在foobar完成之后)。

In simple terms, foo() will entirely execute internally before foobar() starts.简单来说, foo() 将在 foobar() 启动之前完全在内部执行。 However, if within foo() there is a call to execute an ajax request, that request will be submitted, and will then execute in parallel.但是,如果在 foo() 中有执行 ajax 请求的调用,则该请求将被提交,然后将并行执行。

Once the request is submitted the processing will continue.提交请求后,处理将继续。 The ajax request will return with a response when it is complete, that may or may not be picked up by further processing. ajax 请求完成后将返回响应,进一步处理可能会或可能不会收到响应。

Imagine, for example, you have something like this:例如,想象一下,你有这样的东西:

var something;
foo(); //Inside foo, the value of something is set to the result of an AJAX call
console.log(something);

The console.log line will print undefined , because the variable something will still be empty, until the AJAX response in foo changes it. console.log行将打印undefined ,因为变量something仍然是空的,直到foo中的 AJAX 响应改变它。 This is the sort of situation in which a callback is useful - in the callback you can assign the value of the response to something , and you then know that something has a value.在这种情况下,回调很有用——在回调中,您可以将响应的值分配给something ,然后您就知道something有值的。

Your example is very similar to this - foobar will be called as soon as foo returns, and if foo contains some asynchronous logic, it will return before that completes.您的示例与此非常相似 - foobar将在foo返回后立即调用,如果foo包含一些异步逻辑,它将在完成之前返回。 If you want foobar to execute after, you will need to call it from (or use it as) the callback function.如果您希望foobar之后执行,您需要从(或将其用作)回调 function 调用它。

It would be possible to make foo wait until whatever it does has finished, but that would probably defeat the point of using AJAX, as you'd have to send a synchronous request.可以让foo等到它所做的一切完成,但这可能会破坏使用 AJAX 的意义,因为您必须发送同步请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM