Last active
July 27, 2018 12:03
-
-
Save frentsel/53ad9a08e3fc02aed89d6f1612a76a50 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const CustomPromise = function(callback) { | |
const res = (fn, ...args) => fn && callOnce(fn, args); | |
const rej = (fn, ...args) => fn && callOnce(fn, args); | |
let callOnce = (fn, args) => { | |
fn.apply(null, args); | |
callOnce = () => { }; | |
}; | |
this.then = function(resolve, reject) { | |
reject && (this.catch = () => { }); | |
callback( | |
res.bind(null, resolve), | |
rej.bind(null, reject) | |
); | |
return this; | |
} | |
this.catch = function(reject) { | |
callback( | |
function() { }, | |
rej.bind(null, reject) | |
); | |
} | |
} | |
// Use Case | |
new CustomPromise((resolve, reject) => { | |
// setTimeout(resolve, 1000, ['Date', new Date()]); | |
setTimeout(reject, 1000, ['Date', new Date()]); | |
}) | |
.then( | |
console.log.bind(null, 'Resolved:'), | |
console.log.bind(null, 'Rejected:') | |
) | |
.catch(console.log.bind(null, 'Catch:')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment