Skip to content

Instantly share code, notes, and snippets.

@frentsel
Last active July 27, 2018 12:03
Show Gist options
  • Save frentsel/53ad9a08e3fc02aed89d6f1612a76a50 to your computer and use it in GitHub Desktop.
Save frentsel/53ad9a08e3fc02aed89d6f1612a76a50 to your computer and use it in GitHub Desktop.
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