Skip to content

Instantly share code, notes, and snippets.

@joecliff
Last active August 29, 2015 14:00
Show Gist options
  • Save joecliff/7154d1a4a2b43ace31c2 to your computer and use it in GitHub Desktop.
Save joecliff/7154d1a4a2b43ace31c2 to your computer and use it in GitHub Desktop.
recover from error in q.js
'use strict';
var Q = require('q');
function prepare(data) {
// return Q(data);
return Q.reject(new Error('special error1'));
}
function judgeError(err) {
console.log('catch: ', err);
if (~err.message.indexOf('special error').length) {
//simulate calling other async function
return Q('data recovered from error');
}
throw err;
}
function handleResult(data) {
console.log('get data: ', data);
}
prepare('normal data').catch(judgeError).then(handleResult).catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment