I am working on a node.js sails.js app that handles a number of API calls and uses Bluebird for promises. The API calls have been reliable but I would like to build in handling for if they do not respond. I'm wondering if there is a good standard practice for handling timeouts (or a different approach) if the API services are down or do not respond.
Here is the code I have found/modified to work with, 15000 is a 15 second timeout on the API. Is this approach good, is there a better way?
var Promise = require('bluebird');
return new Promise(function (resolve, reject) {
//resolve my API call here.
})
.timeout(15000)
.cancellable()
.catch(Promise.CancellationError, function(error) {
// ... must neatly abort the task ...
console.log('Task cancelled', error);
})
.catch(Promise.TimeoutError, function(error) {
// ... must neatly abort the task ...
console.log('Task timed out', error);
});