Skip to content

Instantly share code, notes, and snippets.

@chimmelb
Last active September 15, 2015 14:45
Show Gist options
  • Save chimmelb/ee349b7fdd824881a2fa to your computer and use it in GitHub Desktop.
Save chimmelb/ee349b7fdd824881a2fa to your computer and use it in GitHub Desktop.
Promise.promisifyAll of a class and errors
start promisifyInLine
calling `a`, willError=false
promisifyInLine from a=123
calling `b`, willError=false
promisifyInLine from b=456
promisifyInLine error=null results=done promisifyInLine
start promisifyUsingAll
calling `a`, willError=false
promisifyUsingAll from a=123
calling `b`, willError=false
promisifyUsingAll from b=456
promisifyUsingAll error=null results=done promisifyUsingAll
Done.
start promisifyInLine
calling `a`, willError=true
Error in promisifyInLine. msg=Bad thing happened in a.
promisifyInLine error=Error: Bad thing happened in a. results=undefined
start promisifyUsingAll
calling `a`, willError=true
Error in promisifyUsingAll. msg=Bad thing happened in a.
promisifyUsingAll error=Error: Bad thing happened in a. results=undefined
Done.
'use strict';
var Promise = require('bluebird');
var async = require('async');
var willError = false;
function SomeClass() {
}
SomeClass.prototype.a = function (callback) {
console.log('calling `a`, willError=' + willError);
if (willError) {
callback(new Error('Bad thing happened in a.'));
return;
}
callback(null, '123');
};
SomeClass.prototype.b = function (callback) {
console.log('calling `b`, willError=' + willError);
if (willError) {
callback(new Error('Bad thing happened in b.'));
return;
}
callback(null, '456');
};
Promise.promisifyAll(SomeClass.prototype);
function promisifyInLine(callback) {
console.log('start promisifyInLine');
var myClass = new SomeClass();
Promise.promisify(myClass.a, myClass)()
.then(function (aResults) {
console.log('promisifyInLine from a=' + aResults);
return Promise.promisify(myClass.b, myClass)();
})
.then(function (bResults) {
console.log('promisifyInLine from b=' + bResults);
callback(null, 'done promisifyInLine');
})
.catch(function (error) {
console.log('Error in promisifyInLine. msg=' + error.message);
callback(error);
});
}
function promisifyUsingAll(callback) {
console.log('start promisifyUsingAll');
var myClass = new SomeClass();
myClass.aAsync()
.then(function (aResults) {
console.log('promisifyUsingAll from a=' + aResults);
return myClass.bAsync();
})
.then(function (bResults) {
console.log('promisifyUsingAll from b=' + bResults);
callback(null, 'done promisifyUsingAll');
})
.catch(function (error) {
console.log('Error in promisifyUsingAll. msg=' + error.message);
callback(error);
});
}
async.series([
function (seriesCb) {
console.log('');
promisifyInLine(function (error, results) {
console.log('promisifyInLine error=' + error + ' results=' + results);
seriesCb(); //ignoring errors to progress series
})
},
function (seriesCb) {
console.log('');
promisifyUsingAll(function (error, results) {
console.log('promisifyUsingAll error=' + error + ' results=' + results);
seriesCb(); //ignoring errors to progress series
})
}
], function () {
console.log('');
console.log('Done.');
process.exit(0);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment