How to propagate a promise error to a higher level of promise after downloading a file?

I am working on an async script loader using bluebird, and I am struggling to pass an error where I can catch it.

When the file loads, I call my method with the name declareas follows:

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

C declare:

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

All this works fine, except that I do not want to have an instruction catchat this moment, because error handling should be done in another module (console.log is just a flag).

:
, declare ? , catch declare , - , - .

!

EDIT:
:

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

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

request , , declare. - ( ). , catch -...

2:
, :

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

, , undefined, :

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

new Promise .

+6
2

!

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

tap , , .catch(console.log).


, -- , Promise . , ! , :

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!
);
+11

request([]) [] callback [], declare() :

declare = function(name, dependency_list, callback) {
    if(!my_lib.callback_dict[name]) {
        my_lib.callback_dict[name] = request(dependency_list).then(function(arr) {
            return Promise.all(arr);
        }).then(function(arr) {
            return callback.apply(window, arr) || window.exports;
        });
    }
    return my_lib.callback_dict[name];
};

, callback - , null (), window.exports.

, , , - .

.

declare(name, dependency_list, callback).then(handleSucces).then(null, handleError);

.then s, , handleSuccess, handleError.

+1

All Articles