Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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