function SimplePromise(promiseFn) {
this.listnerType = 'SimplePromiseListner';
const resolveEvent = new Event(this.listnerType);
 
const resolveFn = function(value) {
console.log('call resolve');
this.value = value;
window.dispatchEvent(resolveEvent);
};
 
promiseFn(resolveFn);
}
 
SimplePromise.prototype.then = function(thenFn) {
const internalThen = function() {
this.value = thenFn(this.value);
};
 
window.addEventListener(this.listnerType, thenFn);
 
// console.log('call then');
// const newValue = thenFn(this.value);
// this.value = newValue;
return this;
};
 
new SimplePromise(resolve => {
console.log('init');
setTimeout(() => {
resolve(null);
console.log('trigger');
}, 3000);
})
.then(value => {
console.log('3 seconds passed', value);
return 'test';
})
.then(value => {
console.log('3 seconds passed', value);
});
 
Click Run to execute JavaScript.
Code runs in an isolated environment.