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

How to use this with p-lazy? #50

Closed
pastelmind opened this issue Jul 21, 2023 · 2 comments
Closed

How to use this with p-lazy? #50

pastelmind opened this issue Jul 21, 2023 · 2 comments

Comments

@pastelmind
Copy link

pastelmind commented Jul 21, 2023

I want a Promise that is cancelable and whose executor is lazily evaluated. Is this possible by composing p-cancelable with p-lazy?

At the moment it seems impossible. If I wrap p-lazy with p-cancelable...

const cancelable = new PCancelable((outerResolve, outerReject, onCancel) => {
  const lazy = new PLazy((innerResolve, innerReject) => {
    // Our executor logic goes here
    // use innerResolve, innerReject, onCancel
  })
  lazy.then(outerResolve, outerReject) // Oops
})

...it would no longer be lazy, because calling lazy.then() triggers the executor. On the other hand, if I wrap p-cancelable with p-lazy...

const lazy = new PLazy((outerResolve, outerReject) => {
  const cancelable = new PCancelable((innerResolve, innerReject, onCancel) => {
    // Our executor logic goes here
    // use innerResolve, innerReject, onCancel
  })
  cancelable.then(outerResolve, outerReject)
})

...then we cannot cancel it because lazy does not have a .cancel() method.

@sindresorhus
Copy link
Owner

No, they don't work with each other. In general, the community is moving towards AbortController (with all its flaws) instead of cancellable promises, so it may be worth adopting that instead in your code.

@pastelmind
Copy link
Author

Thanks. I suppose I could roll my own implementation.

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

2 participants