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

利用类似effect去发送请求 #3

Open
coolpail opened this issue Oct 22, 2020 · 0 comments
Open

利用类似effect去发送请求 #3

coolpail opened this issue Oct 22, 2020 · 0 comments

Comments

@coolpail
Copy link
Owner

function lastPromise(promiseFn) {
  let resetReturn;
  return function () {
    let cancel = false;
    if (resetReturn) resetReturn();
    resetReturn = () => cancel = true
    return new Promise((resolve) => {
      promiseFn().then((res) => !cancel && resolve(res))
    })
  }
}

let count = 1
let fn = () => new Promise(rs => setTimeout(() => rs(count++)))

let lastGn = lastPromise(fn)

lastGn().then(console.log)
lastGn().then(console.log)
lastGn().then(console.log)
  1. 首先闭包一个resetReturn
  2. 每次闭包进一个独立的cancel,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant