Skip to content

Instantly share code, notes, and snippets.

@tranphuoctien
Created March 21, 2016 10:08
Show Gist options
  • Save tranphuoctien/6a6b6800441a3c6bb082 to your computer and use it in GitHub Desktop.
Save tranphuoctien/6a6b6800441a3c6bb082 to your computer and use it in GitHub Desktop.
If you're building your own NodeJS library and you like using Promises, chances are you'll want to implement your module API through promises.
// dual-module.js
var Q = require('q');
module.exports = {
getFullName: function (firstName, lastName, callback) {
var deferred = Q.defer();
if (firstName && lastName) {
var fullName = firstName + " " + lastName;
deferred.resolve(fullName);
}
else {
deferred.reject("First and last name must be passed.");
}
deferred.promise.nodeify(callback);
return deferred.promise;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment