Skipping promise chain after handling error

后端 未结 3 603
星月不相逢
星月不相逢 2021-01-02 11:06

Using the https://github.com/kriskowal/q library, I\'m wondering if it\'s possible to do something like this:

// Module A

function moduleA_exportedFunction(         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 11:46

    It is kind of a design thing. In general, when a module or service returns a promise, you want it to resolve if the call was successful, and to fail otherwise. Having the promise neither resolve or fail, even though you know the call was unsuccessful, is basically a silent failure.

    But hey, I don't know the specifics of your modules, or reasons, so if you do want to fail silently in this case, you can do it by returning an unresolved promise:

    // Module A

    function moduleA_exportedFunction() {
      return promiseReturningService().then(function(serviceResults) {
        if (serviceResults.areGood) {
          // We can continue with the rest of the promise chain
        }
        else {
          performVerySpecificErrorHandling();
          // We want to skip the rest of the promise chain
          return q.defer().promise;
        }
      });
    }
    

提交回复
热议问题