Skip to content

Instantly share code, notes, and snippets.

@jondlm

jondlm/q.js

Created Sep 22, 2014
Embed
What would you like to do?
Node style callbacks to q promises
var Q = require('q');
var assert = require('assert');
// Node style callback
function echo(thing, callback) {
callback(null, thing + thing);
}
//
// Test code
// -------------------------------------
// Standard node version
echo('hello', function(err, result) {
assert.strictEqual(result, 'hellohello');
console.log('good');
});
// Using the `call` promise approach
Q.nfcall(echo, 'there').then(function(result) {
assert.strictEqual(result, 'therethere');
console.log('good');
}).fail(function(err) {
console.log(err);
});
// Creating a reusable promise wrapper
var echoPromised = Q.denodeify(echo);
echoPromised('sir').then(function(result) {
assert.strictEqual(result, 'sirsir');
console.log('good');
}).fail(function(err) {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.