• <bdo id='KAUEh'></bdo><ul id='KAUEh'></ul>

    <small id='KAUEh'></small><noframes id='KAUEh'>

    <i id='KAUEh'><tr id='KAUEh'><dt id='KAUEh'><q id='KAUEh'><span id='KAUEh'><b id='KAUEh'><form id='KAUEh'><ins id='KAUEh'></ins><ul id='KAUEh'></ul><sub id='KAUEh'></sub></form><legend id='KAUEh'></legend><bdo id='KAUEh'><pre id='KAUEh'><center id='KAUEh'></center></pre></bdo></b><th id='KAUEh'></th></span></q></dt></tr></i><div id='KAUEh'><tfoot id='KAUEh'></tfoot><dl id='KAUEh'><fieldset id='KAUEh'></fieldset></dl></div>
      <tfoot id='KAUEh'></tfoot>

    1. <legend id='KAUEh'><style id='KAUEh'><dir id='KAUEh'><q id='KAUEh'></q></dir></style></legend>

      1. 为什么 Promise 库使用事件循环?

        时间:2023-08-25
        <legend id='DKgtm'><style id='DKgtm'><dir id='DKgtm'><q id='DKgtm'></q></dir></style></legend>
        <i id='DKgtm'><tr id='DKgtm'><dt id='DKgtm'><q id='DKgtm'><span id='DKgtm'><b id='DKgtm'><form id='DKgtm'><ins id='DKgtm'></ins><ul id='DKgtm'></ul><sub id='DKgtm'></sub></form><legend id='DKgtm'></legend><bdo id='DKgtm'><pre id='DKgtm'><center id='DKgtm'></center></pre></bdo></b><th id='DKgtm'></th></span></q></dt></tr></i><div id='DKgtm'><tfoot id='DKgtm'></tfoot><dl id='DKgtm'><fieldset id='DKgtm'></fieldset></dl></div>

          <small id='DKgtm'></small><noframes id='DKgtm'>

            <tfoot id='DKgtm'></tfoot>
              <bdo id='DKgtm'></bdo><ul id='DKgtm'></ul>
                  <tbody id='DKgtm'></tbody>

                • 本文介绍了为什么 Promise 库使用事件循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下 JavaScript 代码:

                  var promise = new Promise();设置超时(功能(){承诺.resolve();}, 10);函数 foo() { }承诺.then(foo);

                  在我见过的承诺实现中,promise.resolve() 会简单地设置一些属性来指示承诺已解决,并且稍后将在事件循环中调用 foo(),但它看起来像 promise.resolve() 将有足够的信息来立即调用任何延迟函数,例如 foo().

                  事件循环方法似乎会增加复杂性并降低性能,那么为什么要使用它?

                  虽然我对 Promise 的大部分使用是与 JavaScript 一起使用的,但我提出问题的部分原因是在 C++ 游戏等性能非常密集的情况下实现 Promise,在这种情况下,我想知道是否可以利用没有事件循环开销的承诺.

                  解决方案

                  所有 promise 实现,至少是好的实现.

                  这是因为将同步性混合到异步 API 中是发布 Zalgo.>

                  事实承诺有时不会立即解决,而延迟有时意味着 API 是一致的.否则,您将按照执行顺序获得未定义的行为.

                  function getFromCache(){返回 Promise.resolve(cachedValue || getFromWebAndCache());}getFromCache().then(function(x){警报(世界");});警报(你好");

                  fact promise libraries defer,意味着上面块的执行顺序是有保证的.在像 jQuery 这样的违背承诺的实现中,顺序会根据项目是否从缓存中获取而改变.这很危险.

                  具有不确定的执行顺序是非常危险的,并且是错误的常见来源.Promises/A+ 规范把你扔进了成功的坑里.

                  Considering the following JavaScript code:

                  var promise = new Promise();
                  setTimeout(function() {
                      promise.resolve();
                  }, 10);
                  
                  function foo() { }
                  promise.then(foo);
                  

                  In the promise implementations I've seen, promise.resolve() would simply set some property to indicate the promise was resolved and foo() would be called later during an event loop, yet it seems like the promise.resolve() would have enough information to immediately call any deferred functions such as foo().

                  The event loop method seems like it would add complexity and reduce performance, so why is it used?

                  While most of my use of promises is with JavaScript, part of the reason for my question is in implementing promises in very performance intensive cases like C++ games, in which case I'm wondering if I could utilize some of the benefits of promises without the overhead of an event loop.

                  解决方案

                  All promise implementations, at least good ones do that.

                  This is because mixing synchronicity into an asynchronous API is releasing Zalgo.

                  The fact promises do not resolve immediately sometimes and defer sometimes means that the API is consistent. Otherwise, you get undefined behavior in the order of execution.

                  function getFromCache(){
                        return Promise.resolve(cachedValue || getFromWebAndCache());
                  }
                  
                  getFromCache().then(function(x){
                       alert("World");
                  });
                  alert("Hello");
                  

                  The fact promise libraries defer, means that the order of execution of the above block is guaranteed. In broken promise implementations like jQuery, the order changes depending on whether or not the item is fetched from the cache or not. This is dangerous.

                  Having nondeterministic execution order is very risky and is a common source of bugs. The Promises/A+ specification is throwing you into the pit of success here.

                  这篇关于为什么 Promise 库使用事件循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:来自 std::promise 的未知异常 下一篇:mmap() 与读取块

                  相关文章

                  1. <legend id='i7nAe'><style id='i7nAe'><dir id='i7nAe'><q id='i7nAe'></q></dir></style></legend>
                    <i id='i7nAe'><tr id='i7nAe'><dt id='i7nAe'><q id='i7nAe'><span id='i7nAe'><b id='i7nAe'><form id='i7nAe'><ins id='i7nAe'></ins><ul id='i7nAe'></ul><sub id='i7nAe'></sub></form><legend id='i7nAe'></legend><bdo id='i7nAe'><pre id='i7nAe'><center id='i7nAe'></center></pre></bdo></b><th id='i7nAe'></th></span></q></dt></tr></i><div id='i7nAe'><tfoot id='i7nAe'></tfoot><dl id='i7nAe'><fieldset id='i7nAe'></fieldset></dl></div>

                    <tfoot id='i7nAe'></tfoot>

                    <small id='i7nAe'></small><noframes id='i7nAe'>

                      <bdo id='i7nAe'></bdo><ul id='i7nAe'></ul>