Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

javascript - ajax async:false选项如何在后台运行? [重复](How does ajax async: false option work under the hood? [duplicate])

When I make an ajax call and pass async: false , it makes an asynchronous request but it stop the code execution until the request is done (making it synchronous).(当我进行ajax调用并传递async: false ,它将发出异步请求,但会停止执行代码,直到请求完成(使其成为同步)。)

How does it work under the hood?(它是如何工作的?)

How can it make a async request become synchronous?(如何使异步请求变为同步?)

I'm trying to implement the same logic and want to understand better how it works.(我正在尝试实现相同的逻辑,并希望更好地了解其工作原理。)

  ask by Vencovsky translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

jQuery uses the XMLHttpRequest object.(jQuery使用XMLHttpRequest对象。)

It has a synchronous mode built-in (which is handled in native code).(它具有内置的同步模式(以本机代码处理)。)

As you noticed, it blocks all other code execution.(如您所见,它阻止了所有其他代码的执行。)

As a result of this that feature is deprecated and should not be used.(因此,该功能已被弃用 ,不应使用。)

Note that this isn't converting asynchronous code to synchronous code, there is no technique which can do that in the general case (although await can give the illusion of synchronous code within promise).(请注意,这并不是将异步代码转换为同步代码,在一般情况下,没有任何一种技术可以做到这一点(尽管await可以使承诺中的同步代码产生错觉)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...