Show HN: LazyPromise = Observable – Signals
29 points
5 days ago
| 5 comments
| github.com
| HN
A Promise-like primitive which is lazy/cancelable, has typed errors, and emits synchronously instead of on the microtask queue. In a way LazyPromise is what you get if you take an Observable and make it impossible to misuse it for what the Signals were built to do.
guntis_dev
3 hours ago
[-]
Quick question - would something like this cover the basic cancelable promise use case, or am I missing something important about what LazyPromise does differently?

  function cancelablePromise() {
    const promise = new Promise(resolve => setTimeout(resolve, 1000))
  
    let cancel: () => void
    const cancelPromise = new Promise((_, reject) => {
      cancel = () => reject("promise canceled")
    })
  
    const wrappedPromise = Promise.race([promise, cancelPromise])
  
    return {
      promise: wrappedPromise,
      cancel: cancel,
    }
  }
reply
conartist6
5 hours ago
[-]
Interesting, interesting. It would take me a few hours of playing with this mechanism to know what I think of it as a primitive, but for me this solution is relevant to a problem I really have so I might take a look. Right now I just write methods that sometimes return a promise and sometimes don't.
reply
nullzzz
3 hours ago
[-]
Interesting stuff!

The two separate failure channels turn me off. Is this practical or does it introduce unwanted complexity in most cases?

Also wondering what are the Signals that are mentioned.

reply
lgas
4 hours ago
[-]
Do you want monads? Because this is how you get monads.
reply
MuffinFlavored
5 hours ago
[-]
> except you can optionally return a teardown function, for example:

Kind of reminds me of https://doc.rust-lang.org/rust-by-example/trait/drop.html

reply