返回承诺与返回未定义内部的未定义之间的区别 [英] Difference between returning a promise vs returning undefined inside a promise

查看:46
本文介绍了返回承诺与返回未定义内部的未定义之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否了解这两种常见方案之间的区别.

说我们有这个

  user.save().then(function(val){anotherPromise1(val);}).then(function(val){anotherPromise2(val);}).catch(function(err){}); 

与之相对:

  user.save().then(function(val){返回anotherPromise1(val);}).then(function(val){返回anotherPromise2(val);}).catch(function(err){}); 

我知道这会有所作为,但是到底有什么作用?

解决方案

如果您没有从 then 回调中返回值,则实际上是在返回 undefined .下一个 then 回调将立即运行,并且将 undefined 作为分辨率值.

如果您从 then 回调返回承诺,则第二个 then 回调将等待该承诺(间接地,但这并不重要),以及何时承诺已解决,并从该承诺中获得了解决方案的价值.

(承诺/A +规范中的 then 规范对此进行了说明,但略有遗漏—并没有明确提及 onFulfilled 不返回任何内容会发生什么,但是在JavaScript中,调用函数 always 给出了您将得到一个结果值;如果该函数未显式返回某些内容,则调用它的结果为 undefined .JavaScript没有 void 方法的概念C/C#/C ++/Java.)

您可以在此脚本中看到它

输出为(例如):

0015:anotherPromise [没有]得到420017:anotherPromise2 [without]不确定0018:全部完成0020:anotherPromise [with]得到421021年:anotherPromise2 [with]得到了841032:全部完成

请注意返回和返回之间的区别:

I am not really sure I understand the difference between these two common scenarios.

Say we have this:

user.save().then(function(val){
   anotherPromise1(val);
}).then(function(val){
   anotherPromise2(val);
}).catch(function(err){

});

versus:

user.save().then(function(val){
   return anotherPromise1(val);
}).then(function(val){
   return anotherPromise2(val);
}).catch(function(err){

});

I know it makes a difference but how exactly?

解决方案

If you don't return a value from the then callback, you're effectively returning undefined. The next then callback will run immediately, and see undefined as the resolution value.

If you return a promise from the then callback, the second then callback waits on that promise (indirectly, but that doesn't really matter), and when that promise is resolved, gets the resolution value from that promise.

(This is covered by the then specification in the Promises/A+ spec, but slightly by omission — it doesn't explicitly mention what should happen if onFulfilled doesn't return anything, but in JavaScript, calling a function always gives you a resulting value; if the function doesn't explicitly return something, undefined is the result of calling it. JavaScript doesn't have the concept of void methods a'la C/C#/C++/Java.)

You can see it in this script live copy on Babel's REPL:

let start = Date.now();
function elapsed() {
  let rv = String(Date.now() - start);
  while (rv.length < 4) {
    rv = "0" + rv;
  }
  return rv;
}
function anotherPromise(type, val) {
  console.log(`${elapsed()}: anotherPromise[${type}] got ${val}`);
  return new Promise(resolve => {
    setTimeout(() => { resolve(val * 2); }, 1000);
  });
}
function anotherPromise2(type, val) {
  console.log(`${elapsed()}: anotherPromise2[${type}] got ${val}`);
  return new Promise(resolve => {
    setTimeout(() => { resolve(val * 3); }, 10);
  });
}
let user = {
  save: () => {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(42);
      }, 10);
    });
  }
}

// Without return
user.save().then(function(val){
   anotherPromise("without", val);
}).then(function(val){
   anotherPromise2("without", val);
}).then(function() {
  console.log(`${elapsed()}: All done`);
}).catch(function(err){
});

user.save().then(function(val){
   return anotherPromise("with", val);
}).then(function(val){
   return anotherPromise2("with", val);
}).then(function() {
  console.log(`${elapsed()}: All done`);
}).catch(function(err){
});

The output is (for example):

0015: anotherPromise[without] got 42
0017: anotherPromise2[without] got undefined
0018: All done
0020: anotherPromise[with] got 42
1021: anotherPromise2[with] got 84
1032: All done

Note the differences between without a return and with a return:

  • Without, anotherPromise2 was called immediately (as we can see from the elapsed time values) and received undefined.

  • With, anotherPromise2 waited for anotherPromise's resolution to occur, and then received 84 (anotherPromise's resolution value)

这篇关于返回承诺与返回未定义内部的未定义之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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