本文介绍了Java承诺是否会阻止堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 使用Javascript Promise时,事件循环会被阻止吗? 我的理解是,使用await&异步,使堆栈停止直到操作完成。 解决方案 使用Javascript Promise时,事件循环会被阻止吗? 否。承诺只是一个事件通知系统。他们本身不是手术。他们只是通过调用适当的 .then()或 .catch()处理程序来响应被解决或拒绝的情况,如果链接到其他promise,他们可以延迟调用这些处理程序,直到链接它们的promise也解决/拒绝为止。因此,一个承诺不会阻止任何事情,当然也不会阻止事件循环。 我的理解是使用await&异步,使堆栈停止直到操作完成。它是通过阻塞堆栈来做到这一点,还是起到类似于回调并将流程传递给某种API的作用? await 只是语法糖,它用更简单的语法替换了 .then()处理程序。但是,在幕后操作是相同的。 await 之后的代码基本上放在不可见的 .then()处理程序中,并且不会阻塞事件循环,就像使用 .then()处理程序没有阻塞一样。 请注意解决以下注释之一: 现在,如果您要构建的代码以不断解决的承诺压倒了事件循环(在某些无限循环中,如此处一些注释中所建议) ),那么事件循环将一遍又一遍地处理来自微任务队列的那些持续解决的承诺,并且永远不会有机会处理在事件循环中等待的宏任务(其他类型的事件)。事件循环仍在运行,并且仍在处理微任务,但是如果您不断向其中填充新的微任务(已解决的Promise),则它可能永远无法到达宏任务。似乎有人争论是否将其称为阻止事件循环。或不。那只是一个术语问题-更重要的是实际发生的事情。在此示例中,无限循环不断地解决新的诺言,事件循环将继续处理这些已解决的诺言,并且事件队列中的其他事件将不会得到处理,因为它们从不走到最前面以获取其诺言。转。这通常被称为饥饿。而不是阻塞,但要点是,如果您连续不断地将新的微任务放入队列中,则宏任务可能无法得到服务。 这种无限循环的概念不断得到解决。使用Javascript应该避免新的承诺。它可能会因为无法获得服务而饿死其他事件。 When using Javascript promises, does the event loop get blocked?My understanding is that using await & async, makes the stack stop until the operation has completed. Does it do this by blocking the stack or does it act similar to a callback and pass of the process to an API of sorts? 解决方案When using Javascript promises, does the event loop get blocked?No. Promises are only an event notification system. They aren't an operation themselves. They simply respond to being resolved or rejected by calling the appropriate .then() or .catch() handlers and if chained to other promises, they can delay calling those handlers until the promises they are chained to also resolve/reject. As such a single promise doesn't block anything and certainly does not block the event loop.My understanding is that using await & async, makes the stack stopuntil the operation has completed. Does it do this by blocking thestack or does it act similar to a callback and pass of the process toan API of sorts?await is simply syntactic sugar that replaces a .then() handler with a bit simpler syntax. But, under the covers the operation is the same. The code that comes after the await is basically put inside an invisible .then() handler and there is no blocking of the event loop, just like there is no blocking with a .then() handler.Note to address one of the comments below:Now, if you were to construct code that overwhelms the event loop with continually resolving promises (in some sort of infinite loop as proposed in some comments here), then the event loop will just over and over process those continually resolved promises from the microtask queue and will never get a chance to process macrotasks waiting in the event loop (other types of events). The event loop is still running and is still processing microtasks, but if you are stuffing new microtasks (resolved promises) into it continually, then it may never get to the macrotasks. There seems to be some debate about whether one would call this "blocking the event loop" or not. That's just a terminology question - what's more important is what is actually happening. In this example of an infinite loop continually resolving a new promise over and over, the event loop will continue processing those resolved promises and the other events in the event queue will not get processed because they never get to the front of the line to get their turn. This is more often referred to as "starvation" than it is "blocking", but the point is that macrotasks may not get serviced if you are continually and infinitely putting new microtasks in the queue.This notion of an infinite loop continually resolving a new promise should be avoided in Javascript. It can starve other events from getting a chance to be serviced. 这篇关于Java承诺是否会阻止堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 11:15