Skip to content

Instantly share code, notes, and snippets.

@mohsen1
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohsen1/f628e595497ee383ace8 to your computer and use it in GitHub Desktop.
Save mohsen1/f628e595497ee383ace8 to your computer and use it in GitHub Desktop.
Promises and callback
function add (arg1, arg2, cb) {
return (new Promise(reject, resolve) {
if (typeof arg1 !== 'number') {
reject(new TypeError('arg1 should be a number'));
}
if (typeof arg2 !== 'number') {
reject(new TypeError('arg2 should be a number'));
}
resolve(arg1 + arg2);
}).then(function (result){ cb(null, result);}, cb);
}
function add (arg1, arg2, cb) {
return new Promise(reject, resolve) {
if (cb) {
reject = function (err) {
cb(err)
};
resolve = function (result) {
cb(null, result);
}
}
if (typeof arg1 !== 'number') {
reject(new TypeError('arg1 should be a number'));
}
if (typeof arg2 !== 'number') {
reject(new TypeError('arg2 should be a number'));
}
resolve(arg1 + arg2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment