setTimeout 和 this.async 不一样?

setTimeout not the same as this.async?

有时我以错误的方式编码,我的 Polymer 1.0 网络应用程序和其他东西停止正常工作。就像将数据设置为某个自定义元素,然后立即尝试从中读取一些数据(这取决于刚刚设置的数据)(因为我不知道更好)。有时这不起作用。大多数时候 this.async 会帮助我,但有时不会。但是,setTimeout 在这种情况下从未让我失望过。大多数情况下调用 setTimeout 而不提供等待时间也同样有效。

很长一段时间我都认为this.async(function(){...})setTimeout(function(){...})是一样的。因为有时 this.async 内的代码无法看到自定义元素数据的变化,而 setTimeout 内的代码则不会。

这两种方法的实现方式不同吗?

this.async 将您的函数添加到事件队列的开头,而 setTimeout 将其添加到末尾。如果您使用 setTimeout 其他函数可能在您的函数被调用之前已经执行,导致您可以在数据中看到的变化。

来自this.asyncdocumentation

If no wait time is specified, runs tasks with microtask timing (after the current method finishes, but before the next event from the event queue is processed)

另一方面,

setTimeout 会将您的函数添加到队列末尾,如本 article.

部分 "Adding Messages" 中所述

Calling setTimeout will add a message to the queue after the time passed as second argument. If there is no other message in the queue, the message is processed right away; however, if there are messages, the setTimeout message will have to wait for other messages to be processed. For that reason the second argument indicates a minimum time and not a guaranteed time