javascript - How to propagate a promise error to a higher promise level after loading a file? -


i'm working on async script loader using bluebird , i'm struggling pass error can catch it.

when file loaded i'm calling method named declare this:

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

with declare being:

declare = function (name, dependency_list, callback) {    var resolver;     // digest promises returned each module    function digestdependencyarray(my_dependency_array) {      var i, len, response_array;       len = my_dependency_array.length;      (i = 0, response_array = []; < len; += 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 used when integrating commonjs modules       if (!return_value) {         return_value = window.exports;       }        resolver(return_value);    }     // start: set callback (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 works fine except not have catch statement @ point, because error handling should done in different module (the console.log flag).

question:
how propagate error occuring in declare method higher promise chain? had hoped adding catch handler on declare calls help, breaks whole script - assume because i'm returning module declare call vs valid promise response.

thanks tips!

edit:
i'm calling declare this:

 request([{"name": "foo", "src": "path/to/foo.js"}])    .spread(foo) {   })  .catch(function (e) {    console.log(e);  }) 

request load file inside promise gets resolved when file loaded , run file content callback, calls above declare method. somehow error lost on way (code here). let's see if can catch somewhere...

edit 2:
changing 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 no errors, requesting multiple modules not work because modules 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 new promise returning file once it's loaded.

you need rethrow error!

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

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


also, using manually-construct-promise antipattern, while should never need call promise constructor. use promise have! seems want 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 global `resolver()`                                     // `return` value! ); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -