如何传播的承诺错误到一个更高的水平的承诺加载文件后? [英] How to propagate a promise error to a higher promise level after loading a file?

查看:168
本文介绍了如何传播的承诺错误到一个更高的水平的承诺加载文件后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用蓝鸟上的异步加载脚本工作,我挣扎传递一个错误到哪里可以抓住它。

I'm working on an async script loader using bluebird and I'm struggling to pass an error up to where I can catch it.

当一个文件被加载,我打电话给我的方法命名为声明是这样的:

When a file is loaded I'm calling my method named declare like this:

  declare("storage", [
    {"name": 'util', "src": '../src/util.js'}
  ], function (util) {
    var storage = {};
    //...stuff with util
    return storage;
  });

使用声明之中:

declare = function (name, dependency_list, callback) {
   var resolver;

   // digest promises returned for each module
   function digestDependencyArray(my_dependency_array) {
     var i, len, response_array;

     len = my_dependency_array.length;
     for (i = 0, response_array = []; i < len; i += 1) {
       response_array[i] = my_dependency_array[i];
     }

     return Promise.all(response_array);
   }

   // resolve request promise
   function resolveDependencyArray(my_fullfillment_array) {
      var return_value = callback.apply(window, my_fullfillment_array);

      // window.exports must be used when integrating commonjs modules
      if (!return_value) {
        return_value = window.exports;
      }

      resolver(return_value);
   }

   // START: set callback to (resolved) callback or new promise
   my_lib.callback_dict[name] = my_lib.callback_dict[name] ||
      new Promise(function (resolve) {
        resolver = resolve;
        if (dependency_list.length === 0) {
          return resolver(callback.apply(window));
        }

        return request(dependency_list)
          .then(digestDependencyArray)
          .then(resolveDependencyArray)
          // DON'T CATCH HERE...
          .catch(console.log);
      });
  };

这一切工作正常,除了我想不会有语句在这一点上,因为错误处理应该在不同的模块来完成(在执行console.log仅仅是一个标志)。

This all works fine except I would like to not have the catch statement at this point, because the error handling should be done in a different module (the console.log is just a flag).

:结果
我怎么会在传播我的声明方法错误发生的历史到一个更高的诺言链?我曾希望加入处理我的声明通话会有所帮助,但是这打破了整个脚本 - 我猜是因为我是从我的申报调用返回模块VS有效承诺响应。

Question:
How would I propagate an error occuring in my declare method to a higher promise chain? I had hoped adding a catch handler on my declare calls would help, but this breaks the whole script - I assume because I'm returning the module from my declare call vs a valid promise response.

感谢您的任何提示!

修改:结果
我打电话从这个声明:

EDIT:
I'm calling declare from this:

 request([{"name": "foo", "src": "path/to/foo.js"}])
   .spread(foo) {

 })
 .catch(function (e) {
   console.log(e);
 })

要求将加载当文件被加载并运行该文件内容的回调,然后调用上面的声明方法。不知怎的,我的错误(丢失在路上code 这里)。让我们来看看,如果我能它的地方...

request will load the file inside a promise which gets resolved when the file is loaded and run the file content as callback, which then calls the above declare method. Somehow my error is lost on the way (code here). Let's see if I can catch it somewhere...

编辑2 :结果
改变这种内声明的:

Edit 2:
Changing to this inside declare:

function resolveDependencyArray(my_fullfillment_array) {
  var return_value = callback.apply(window, my_fullfillment_array);

  if (!return_value) {
    return_value = window.exports;
  }
  return return_value;
}

function handler() {
  if (dependency_list.length === 0) {
    Promise.resolve(callback.apply(window));
  } else {
    return request(dependency_list)
      .then(digestDependencyArray)
      .then(resolveDependencyArray)
      .catch(function (e) {
        reject(e);
      });
  }
}

clappjs.callback_dict[name] = clappjs.callback_dict[name] || handler();

虽然我没有得到任何错误,请求多个模块不能正常工作,因为返回模块未定义,所以:

request(["foo", "bar", "baz"]).spread(function (foo, bar, baz) {
 console.log(foo);  // undefined
 console.log(bar);  // {} OK
 console.log(baz);  // undefined
});

与一个新无极一旦它的加载正确返回文件。

versus a new Promise properly returning the file once it's loaded.

推荐答案

您需要重新抛出错误!

.catch(function(e) {
  console.log(e); // calling it as a method, btw
  throw e;
})

您也可以尝试使用 自来水 ,或添加 .catch(执行console.log)之前返回您链有承诺

You also might try to use tap, or return the promise that you have in the chain before adding .catch(console.log).

此外,您使用的是 manually-构建-承诺反模式,而你其实应该永远不需要<一个href=\"https://github.com/petkaantonov/bluebird/blob/master/API.md#new-promisefunctionfunction-resolve-function-reject-resolver---promise\"相对=nofollow>致电无极构造。只需使用你已经拥有的承诺!看来你要做到这一点:

Also, you are using the manually-construct-promise antipattern, while you actually should never need to call the Promise constructor. Just use the promise that you already have! It seems that you want to do this:

my_lib.callback_dict[name] = my_lib.callback_dict[name] || (
  dependency_list.length === 0
  ? Promise.resolve()
  : request(dependency_list)
      .then(digestDependencyArray)
      .then(resolveDependencyArray) // don't call a global `resolver()`
                                    // just `return` the value!
);

这篇关于如何传播的承诺错误到一个更高的水平的承诺加载文件后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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