• <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 库使用事件循环?

        Why do Promise libraries use event loops?(为什么 Promise 库使用事件循环?)
        <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 库使用事件循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)
                  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>

                              <tbody id='i7nAe'></tbody>