catch reject from promise

后端 未结 1 691
清歌不尽
清歌不尽 2021-01-28 00:12

I want to hold the error from func() reject, not direct to onError() by choise,

Before I always let func() reso

相关标签:
1条回答
  • 2021-01-28 00:57

    co supports try/catch:

    co(function* () {
      try{
          yield func();
      }
      catch {
         // if reject catch here, not direct to onError 
      }
    
    
    
    
      yield func();
      // if reject don't catch here just direct to onError
    
    }).then(function (response) {
      response = JSON.stringify(response);
      res.send(response);
    }, function (err) {
      onError(err);
    });
    

    See docs : https://www.npmjs.com/package/co#examples

    0 讨论(0)
提交回复
热议问题