toy
toy

Reputation: 12151

Can I assume that error in a promise will bubble to new Promise and catch that?

I have a function which is going to look up the cache if it finds anything, otherwise then then it will go ahead and fetch the data and set the cache. It's pretty standard. I'm wondering if the error occurs at the most inner function, will it keep bubble up to the outer most Promise? So, I can just have one catch instead of one.

Here's my code.

I'm using Bluebird

 var _self = this;
    return new Promise(function(resolve, reject) {
      _self.get(url, redisClient).then(function getCacheFunc(cacheResponse) {
        if(cacheResponse) {
          return resolve(JSON.parse(cacheResponse));
        }
        webCrawl(url).then(function webCrawl(crawlResults) {
          _self.set(url, JSON.stringify(crawlResults), redisClient);
          return resolve(crawlResults);
        }).catch(function catchFunc(error) {
          return reject(error); // can I delete this catch
        });
      }).catch(function getCacheErrorFunc(cacheError) {
        return reject(cacheError); // and let this catch handle everything?
      });
    });

Upvotes: 5

Views: 3539

Answers (2)

Brian Lauber
Brian Lauber

Reputation: 131

Yes, it is possible to have a single .catch(...) for deeply-nested Promises. The trick: you can resolve a Promise with another Promise. This means that you can refactor your code to:

var _self = this;
_self.get(url, redisClient)
  .then(function(cacheResponse) {
    if(cacheResponse) {
      // Resolve the Promise with a value
      return JSON.parse(cacheResponse);
    }

    // Resolve the Promise with a Promise
    return webCrawl(url)
      .then(function(crawlResults) {
        _self.set(url, JSON.stringify(crawlResults), redisClient);

        // Resolve the Promise with a value
        return crawlResults;
      });
  })
  .catch(function(err) {
    console.log("Caught error: " + err);
  });

Note: I also removed your outermost Promise declaration. This was no longer necessary since _self.get(...) already returned a Promise.

Upvotes: 5

t.niese
t.niese

Reputation: 40862

Assuming that .get returns a Promise you would write it that way:

 var _self = this;
 return _self.get(url, redisClient).then(function(cacheResponse) {
   if (cacheResponse) {
     return JSON.parse(cacheResponse);
   } else {
     return webCrawl(url).then(function(crawlResults) {
       _self.set(url, JSON.stringify(crawlResults), redisClient);
       return crawlResults;
     })
   }
 });

There is no need to introduce a new Promise because you already get one from your _self.get

Upvotes: 3

Related Questions