Created
March 21, 2016 10:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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