Skip to content

Instantly share code, notes, and snippets.

@BiosBoy
Created November 21, 2023 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BiosBoy/231eeb02a5248221e0b63fce76e0ce74 to your computer and use it in GitHub Desktop.
Save BiosBoy/231eeb02a5248221e0b63fce76e0ce74 to your computer and use it in GitHub Desktop.
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