Created
November 21, 2023 13:24
-
-
Save BiosBoy/231eeb02a5248221e0b63fce76e0ce74 to your computer and use it in GitHub Desktop.
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
class MyPromise { | |
constructor(fn) { | |
this._promiseChain = []; | |
fn(this._onResolve, this._onReject); | |
} | |
then = (handleSuccess) => { | |
this._promiseChain.push(handleSuccess); | |
return this; | |
}; | |
catch = (handleError) => { | |
this._handleError = handleError; | |
return this; | |
}; | |
_onResolve = (value) => { | |
let storedValue = value; | |
this._promiseChain.forEach((nextFn) => { | |
storedValue = nextFn(storedValue); | |
}); | |
}; | |
_onReject = (err) => { | |
this._handleError(err); | |
}; | |
} | |
new MyPromise(async (resolve, reject) => { | |
fetch("https://cdn2.thecatapi.com/images/ebv.jpg").then((newData) => { | |
console.log("test01"); | |
resolve(newData); | |
}); | |
}) | |
.then((res) => { | |
console.log("test1"); | |
return res; | |
}) | |
.then((data) => { | |
console.log("test2"); | |
return data; | |
}) | |
.then((data) => { | |
console.log("test3"); | |
return data; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment