create your own Promise
2022年5月1日小于 1 分钟
create your own Promise
Question
Promise is widely used nowadays, hard to think how we handled Callback Hell in the old times.
Can you implement a MyPromise
Class by yourself?
At least it should match following requirements
- new promise:
new MyPromise((resolve, reject) => {})
- chaining :
MyPromise.prototype.then()
then handlers should be called asynchronously - rejection handler:
MyPromise.prototype.catch()
- static methods:
MyPromise.resolve()
,MyPromise.reject()
.
This is a challenging problem. Recommend you read about Promise thoroughly first.
Code
class MyPromise {
constructor(executor) {
// your code here
}
then(onFulfilled, onRejected) {
// your code here
}
catch(onRejected) {
// your code here
}
static resolve(value) {
// your code here
}
static reject(value) {
// your code here
}
}