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

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

问题描述

我正在使用 bluebird 处理异步脚本加载程序,而我正在努力传递错误直到我能抓到它。



当加载文件时,我调用我的方法命名为 declare like这个:

  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;

//为每个模块返回的消化承诺
函数digestDependencyArray(my_dependency_array){
var i,len,response_array;

len = my_dependency_array.length; (i = 0,response_array = []; i {
response_array [i] = my_dependency_array [i]
}

返回Promise.all(response_array);
}

//解析请求promise
函数resolveDependencyArray(my_fullfillment_array){
var return_value = callback.apply(window,my_fullfillment_array);

//在整合commonjs模块时必须使用window.exports
if(!return_value){
return_value = window.exports;
}

解析器(return_value);
}

//开始:设置回调到(已解决)回调或新承诺
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));
}

返回请求(dependency_list)
.then(digestDependencyArray)
.then(resolveDependencyArray)
//不要在这里捕获...
.catch(console.log);
});
};

这一切都可以正常工作,除非我想没有 catch 语句,因为错误处理应该在不同的模块中完成(console.log只是一个标志)。



问题

如何将我的 declare 方法中的错误传播给更高的承诺链?我希望在我的声明中添加一个 catch 处理程序,调用将有所帮助,但这会打破整个脚本 - 我认为是因为我从我的申报电话返回模块和有效的承诺回复。



感谢任何提示!



编辑

我正在从此声明:



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

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

请求将文件加载到承诺中,该文件在加载和运行时得到解决该文件内容作为回调,然后调用上述声明方法。在某种程度上,我的错误丢失了(代码 here )。我们来看看我是否可以在某个地方 catch



编辑2 :< br>
更改为内部声明:

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

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

函数处理程序(){
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] ||处理程序();

虽然我没有收到错误,但请求多个模块不起作用,因为返回模块未定义,所以:

 请求([foo,bar,baz ])。spread(function(foo,bar,baz){
console.log(foo); // undefined
console.log(bar); // {} OK
控制台。 log(baz); // undefined
});

新承诺正确返回文件一旦它被加载。

解决方案

你需要重新抛出错误!

  .catch(function(e){
console.log(e); //调用它作为一种方法,btw
throw e;
} )

您还可以尝试使用 点击 ,或退回您在链中的承诺添加 .catch(console.log)






另外,您正在使用手动构建承诺反模式,而您实际上不应该调用承诺构造函数。只要使用你已经有的承诺!似乎你想这样做:

  my_lib.callback_dict [name] = my_lib.callback_dict [name] || (
dependency_list.length === 0
?Promise.resolve()
:request(dependency_list)
.then(digestDependencyArray)
.then(resolveDependencyArray)/ /不要调用全局`resolver()`
//只是返回值!
);


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;
  });

With declare being:

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);
      });
  };

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).

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.

Thanks for any tips!

EDIT:
I'm calling declare from this:

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

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

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...

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();

While I get no errors, requesting multiple modules does not work because modules are returned undefined, so:

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.

解决方案

You need to rethrow the error!

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

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


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天全站免登陆