当前位置: 首页 > 知识库问答 >
问题:

中返回值和返回promise时,执行顺序有何不同。然后?

李森
2023-03-14

原文是这样的:

Promise.resolve('foo')
  // 1. Receive "foo" concatenate "bar" to it and resolve that to the next then
  .then(function(string) {
    return new Promise(function(resolve, reject) {
      setTimeout(function() {
        string += 'bar';
        resolve(string);
      }, 1);
    });
  })
  // 2. receive "foobar", register a callback function to work on that string
  // and print it to the console, but not before return the unworked on
  // string to the next then
  .then(function(string) {
    setTimeout(function() {
      string += 'baz';
      console.log(string);
    }, 1)
    return string;
  })
  // 3. print helpful messages about how the code in this section will be run
  // before string is actually processed by the mocked asynchronous code in the
  // prior then block.  
  .then(function(string) {
    console.log("Last Then:  oops... didn't bother to instantiate and return " +
                "a promise in the prior then so the sequence may be a bit " +
                "surprising");

    // Note that `string` will not have the 'baz' bit of it at this point. This 
    // is because we mocked that to happen asynchronously with a setTimeout function
    console.log(string);
});

这确实令人惊讶,但最终明白了(嗯,至少我想我明白了)为什么会这样发生。

现在,我将第二个.然后更改为:

  .then(function(string) {
    setTimeout(function() {
      string += 'baz';
      console.log(string);
      return string;
    }, 1)

如果我遗漏了一些明显的东西,或者我的问题是愚蠢的,让我提前说声对不起。还有谢谢你!

共有1个答案

萧懿轩
2023-03-14

我得到最后一个。然后在第二个之前执行。然后返回一个值。

不,您只是没有看到第二个返回值的位置。您的return语句位于settimeout回调中,并从该函数返回(稍后),而不是从then回调返回。事实上,该函数没有任何return语句,因此默认情况下,它在执行主体后返回undefined

你基本上在做

.then(function(string) {
  setTimeout(function() {
    string += 'baz';
    console.log(string);
    return string; // irrelevant for the promise chain
  }, 1)
  return undefined; // this is the value that the promise resolves with
//       ^^^^^^^^^     and which the next `then` callback receives
})
 类似资料:
  • 我对promise不熟悉。我查找了按顺序执行promise的示例,但没有找到与我的应用程序类似的示例。 我的问题是,我想按照一定的顺序解决promise,但也要捕获promise解决时的所有结果。通过查找例子。我已经创建了这个问题的简化版本。 我能够创建一个函数doWork(),但问题是它所做的只是按顺序解决promise,而没有捕获结果。 理想情况下,我想要一个返回promise的函数,该函数将

  • 问题内容: 我想使用这样的Promise来调用Google Maps Geocoding API: 当我调用函数请求时,我发现我得到了一个Promise而不是一个值: 为什么不答应。然后在返回值之前执行?我如何从这个承诺而不是另一个承诺中获得价值? 问题答案: 如果您依赖承诺来返回数据,则必须从函数中返回承诺。 一旦调用堆栈中的1个函数异步,那么要继续线性执行,所有要调用它的函数也必须异步。(异步

  • 问题内容: 我正在尝试使用“ any”匹配器对这个getKeyFromStream方法进行存根。我尝试了更明确和不太明确的(anyObject()),但似乎无论我如何尝试,此存根都不会在我的单元测试中返回fooKey。 我想知道是否是因为它受到保护,或者我缺少其他东西或做错了什么。在整个测试中,我还有其他的when / then语句在 起作用, 但是由于某种原因,事实并非如此。 注意:getKey

  • 我在试用GraphQL。我从postgresql中提取数据,除了一个问题,解析在查询完成之前就完成了,其他一切似乎都能正常工作。我以为我的查询承诺工作正常。这里是我为查询数据库而创建的类: 下面是我的graphql查询: 我注释了我的结果,只是想了解一下返回静态内容的情况,结果似乎在then()内时不会返回到graphisql(这意味着在then()之前完成的解析)。输出: } 我可以通过将静态返

  • 问题内容: 我正在尝试执行上述程序,但为同一程序提供了不同的值。对于给定的字符串执行多次时,是否有任何方法可以获取相同的字节? 问题答案: 在这里,您不打印a的值。正如owlstead在注释中正确指出的那样,将在字节数组上调用Object.toString()方法。导致这种格式的输出: 如果要打印数组中的每个元素,则必须遍历它。 甚至更简单,请使用以下方法:

  • 我正在等待(从USSD请求中)检索一个值,以便返回它(getUSSD):