Skip to content

Instantly share code, notes, and snippets.

@mohsen1

mohsen1/domenic.js

Last active Aug 29, 2015
Embed
What would you like to do?
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