提取.然后



我写了一个常规.js crud对象,该对象保存了所有用于与服务器通信的方法。但是,我想避免重复。然后。然后,我想在外部方法中抽象该功能。

不确定我要实现的目标是否是可能的。

我的代码下面:

all(url, success, fail){
  return new Promise((resolve,reject) => {
    _get(url)
    .then((response) => {
      if (response.status == 200) {
        success.call(this,response);
        return resolve();
      }
    })
    .catch((error) => {
      fail.call(this, error);
      reject(error);
    });
});}, submit, update .....

仙境期望的结果:

all(url, success, fail){
  return new Promise((resolve, reject) => {
    _get(url).handle(args);
  });
}

只需避免使用Promise构造函数antpattern和回调,您就会很好!

function all(url) {
  return _get(url).then((response) => {
    if (response.status == 200) {
      return response;
    }
    // most likely you want to `throw` an error here?
  });
}

相关内容

最新更新