本文介绍了将一个监听器添加到Promise之后,我应该使用原始的Promise还是新的Promise?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些javasript代码,它们接受现有的promise
(例如,fetch()返回的promise)并增加值
(例如,然后/捕获侦听器以进行调试,或者可能更多):

I have some javasript code that takes an existing promise(say, the promise returned by fetch()) and adds value(say, then/catch listeners for debugging, or maybe more):

let myFetch = function(url) {
  return fetch(url).then(function(value) {
    console.log("fetch succeeded: value=",value);
    return value;
  }.catch(function(reason) {
    console.log("fetch failed: reason=",reason);
    throw reason;
  });
};

我发现自己修改了上面的代码,以便仅在满足某些条件的情况下才添加侦听器:

I found myself modifying the above code so that the listeners are added only if some condition is true:

let myFetch = function(url) {
  let promise = fetch(url);
  if (some condition) {
    promise = promise.then(function(value) {
      console.log("fetch succeeded: value=",value);
      return value;
    }.catch(function(reason) {
      console.log("fetch failed: reason=",reason);
      throw reason;
    });
  }
  return promise;
};

现在我想知道,myFetch返回由然后是
(实际上是catch,它实际上是另一个 then的简写),
还是返回原始的诺言(增加了听众)更有意义?
换句话说,我正在考虑省略第二个 promise =,
,以便代码看起来像这样:

Now I'm wondering, does it really make sense for myFetch to return the new promise returned by "then" (actually catch which is shorthand for another "then") as above,or would it make more sense for it to return the original promise (with the added listeners)?In other words, I'm thinking of leaving out the second "promise =",so that the code will look like this instead:

let myFetch = function(url) {
  let promise = fetch(url);
  if (some condition) {
    promise.then(function(value) {
      console.log("fetch succeeded: value=",value);
      return value;
    }.catch(function(reason) {
      console.log("fetch failed: reason=",reason);
      throw reason;
    });
  }
  return promise;
};

与以前的版本实际上不同?
最好使用哪个版本,如果可以,为什么?

Is that effectively different from the previous version?Is either version preferable, and if so, why?

推荐答案

如果您唯一的用例是在其中记录某些内容然后 / 抓住 –只要一切顺利,没关系,如果您遇到了麻烦,事情就会变得更加混乱请考虑以下两个示例:

If your only use case is logging something in then/catch – it shouldn't matter as long as everything goes well. Things get more messed if you get an exception. Consider these two examples:

function myFetch() {
    let promise = new Promise(function (resolve, reject) {
        resolve(100);
    });
    promise.then(function () { throw new Error('x'); });
    return promise;
}

myFetch().then(function () {
    console.log('success!');
}).catch(function (e) {
    console.error('error!', e)
});

结果为成功并引发错误在内部 then 中可能会被某些promise库吞噬(尽管诸如Bluebird之类的最受欢迎的库会处理此问题,并且您会得到其他错误未处理的拒绝错误: x )。
在某些环境中使用。

The result is success and the error thrown in the inner then might get swallowed in some promise libraries (although the most popular ones such as Bluebird handle this and you get additional error Unhandled rejection Error: x).The error might also get swallowed when using native Promises in some environments.

function myFetch() {
    let promise = new Promise(function (resolve, reject) {
        resolve(100);
    });
    promise = promise.then(function () { throw new Error('x'); });
    return promise;
}

myFetch().then(function () {
    console.log('success!');
}).catch(function (e) {
    console.error('error!', e)
});

现在结果是错误!错误:x

这篇关于将一个监听器添加到Promise之后,我应该使用原始的Promise还是新的Promise?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:36