Skip to content
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

取消 promise #37

Open
nmsn opened this issue Aug 24, 2022 · 0 comments
Open

取消 promise #37

nmsn opened this issue Aug 24, 2022 · 0 comments
Labels

Comments

@nmsn
Copy link
Owner

nmsn commented Aug 24, 2022

原文:https://juejin.cn/post/6844904148899463175

function wrap(p) {
  let obj = {};
  let p1 = new Promise((resolve, reject) => {
    obj.resolve = resolve;
    obj.reject = reject;
  });
  obj.promise = Promise.race([p1, p]);
  return obj;
}

let promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(123);
  }, 1000);
});
let obj = wrap(promise);
obj.promise.then(res => {
  console.log(res);
});
obj.resolve("请求被拦截了");

obj.reject("请求被拒绝了");
@nmsn nmsn added the ES6 label Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant