是什么确定使用promise或setTimeout的延迟函数的调用顺序? [英] What determines the call order of deferred function using promises or setTimeout?

查看:62
本文介绍了是什么确定使用promise或setTimeout的延迟函数的调用顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

延迟功能的执行(例如在自定义事件处理中)是JavaScript中的常见模式(例如,参见此处)。过去,使用 setTimeout(myFunc,0)是实现此目的的唯一方法,但是可以保证,现在有了替代方法: Promise。 resolve()。then(myFunc)



我以为它们几乎可以做同样的事情,但是在处理包含自定义事件的库时,我想我会发现是否存在差异,因此将以下代码块放入节点:

  var logfn = function(v){return function(){console.log (v)}}; 

setTimeout(logfn(1),0);
Promise.resolve()。then(logfn(2));
logfn(3)();

我原本希望在控制台上看到3、1、2,但我看到了3、2, 1.因此换句话说,Promise不等同于使用setTimeout,并且首先出现在块中。至少在Node中。



我在Chrome和Firefox中重复了测试,结果相同,但是在Edge中,结果为3、1、2。我也希望非本地的Promise库在后台使用setTimeout,因此与Edge相同。



什么决定了这些调用的解决顺序?这些不同的环境使用什么模型来确定执行顺序?



PS我明确地不建议依赖任何这种持久性,我只是很好奇。 / p>




在给出以下答案后,我指出了正确的方向,并在下面的评论中简要提到,我找到了完整的可以在杰克·阿奇博尔德(Jake Archibald)的出色文章中找到答案(示例几乎相同到我上面的代码),尽管我会在这里加起来而不是将其埋在注释中。

解决方案

ll取决于 resolve()是如何在内部实现的-可能您观察到 setTimeout(fn,0)与Edge实现的区别 setImmediate(fn)



请考虑以下文章- http://www.mattgreer.org/articles/promises-in-wicked-detail/ 以及解析方法的实现方式。

  function resolve(value){
//强制在下一个
中调用回调//事件循环的结束,给
//回调函数一个机会,然后由then()
setTimeout(function(){
callback(value);
},1);
}

setTimeout与setImmediate之间的优先级



来自Microsoft文档- https:// developer。 microsoft.com/en-us/microsoft-edge/platform/documentation/dev-guide/performance/efficiency-script-yielding/ 和另一个链接- setImmediate方法


Deferring the execution of functions, for example in custom event handling, is a common pattern in JavaScript (see, for example here). It used to be that using setTimeout(myFunc,0) was the only way to do this, however with promises there is now an alternative: Promise.resolve().then(myFunc).

I had assumed that these would pretty much do the same thing, but while working on a library which included custom events I thought I'd find out if there was a difference, so I dropped the following block into node:

var logfn=function(v){return function(){console.log(v)}};

setTimeout(logfn(1),0);
Promise.resolve().then(logfn(2));
logfn(3)();

I was expecting to see on the console 3, 1, 2 but instead I saw 3, 2, 1. So in other words the Promise is NOT equivalent to using setTimeout and comes out of the blocks first. At least in Node.

I repeated the test in Chrome and Firefox with the same result, however in Edge it came out as 3, 1, 2. I would also expect non-native promise libraries to use setTimeout under the hood so would come out the same as Edge.

What determines the order of these calls being resolved? What model is used by these different environments to determine execution order? Does any of the above represent standard or non-standard behaviour?

PS I'm defniately not suggesting relying on any of this staying consistant, I'm just curious.


After the answer given below pointed me in the right direction, and as mentioned briefly in the comment below, I found the complete answer in an excellent article by Jake Archibald (with an example almost identical to my code above) which I though I'd add up here rather than leaving it buried in a comment.

解决方案

ll depends how resolve() was internally implemented - probably you observe difference between setTimeout(fn, 0) and Edge implementations for setImmediate(fn)

Please consider the article - http://www.mattgreer.org/articles/promises-in-wicked-detail/ and the way how resolve method was implemented.

function resolve(value) {
    // force callback to be called in the next
    // iteration of the event loop, giving
    // callback a chance to be set by then()
    setTimeout(function() {
        callback(value);
    }, 1);
}

Than some explenations can be found at priority between setTimeout and setImmediate

From Microsoft documentation - https://developer.microsoft.com/en-us/microsoft-edge/platform/documentation/dev-guide/performance/efficient-script-yielding/ and one more link - setImmediate method

这篇关于是什么确定使用promise或setTimeout的延迟函数的调用顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆