Last active
August 29, 2015 14:20
-
-
Save mohsen1/f628e595497ee383ace8 to your computer and use it in GitHub Desktop.
Promises and callback
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
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); | |
} |
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
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