Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function nextTick(callback) {
setTimeout(callback, 0);
}
class Promise {
constructor(callback) {
this.state = 'pending';
this.onSuccess = [];
callback(value => {
this.state = 'resolved';
this.value = value;
this.onSuccess.forEach(callback => callback(value));
});
}
then(callback) {
return new Promise(resolve => {
if (this.state === 'resolved') {
nextTick(() => resolve(callback(this.value)));
} else {
this.onSuccess.push(value => resolve(callback(value)));
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment