【问题标题】:Why does When.js promise.then skip a function?为什么When.js promise.then 跳过一个函数?
【发布时间】:2015-05-20 06:25:10
【问题描述】:

有人可以解释为什么会以相反的顺序打印吗?

代码:

when('test')
  .then(function() {console.log('should be first');})
  .then(console.log('should be second'));

输出:

should be second
should be first

PS:我用的是when.js版本:when@3.4.3

【问题讨论】:

    标签: javascript promise when-js


    【解决方案1】:

    您将立即执行第二个console.log,并将返回值传递给then。您需要将函数传递给then

    你已经有效地做到了:

    var x = console.log('should be second')
    
    when('test')
      .then(function () { console.log('should be first'); })
      .then(x);
    

    【讨论】:

    • 值得一提 - 像 WhenJS 这样的 /A+ Promise 库保证这是总是事情发生的顺序。在某些 Promise 实现中,您没有该保证(.then 中的代码始终异步运行)
    猜你喜欢
    • 2017-11-10
    • 2016-05-27
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    相关资源
    最近更新 更多