-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2. 实现Promise.finally #2
Comments
|
Promise.prototype.myfinally = function (callback) {
return this.then(
async (res) => {
await callback();
return res; //finally本质起传递的作用,这里的res是上一个then函数的返回值
},
async (err) => {
await callback();
throw err;
}
);
};
Promise.resolve(123)
.then((res) => {
console.log(res); //123
return Promise.reject(456);
})
.myfinally(() => {
console.log("finally");
return "finally本身不返回值";
})
.then(
() => {},
(err) => {
console.log(err); //123
return 789;
}
)
.myfinally(() => console.log("finally"))
.then((res) => console.log(res)); //789 |
Promise.prototype.Finally = function (fn) { |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: