top | item 46321852

Show HN: LazyPromise = Observable – Signals

34 points| ivan7237d | 2 months ago |github.com

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.

10 comments

order

guntis_dev|2 months 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,
    }
  }

ivan7237d|2 months ago

Yeah, I think this will cover the basic use-case, although the way I'd approach this is by starting with AbortSignal API and then maybe creating a wrapper around it. In a way `eager` and `lazy` functions from LazyPromise library are such wrappers.

nullzzz|2 months 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.

ivan7237d|2 months ago

Author here. Good questions.

About the two error channels: what I tried to do is to make it really easy to not use typed errors when you don't need them. In that case your promise is typed as `LazyPromise<Value, never>`.

Signals is a reactive primitive that has been initially introduced in SolidJS framework and have then made their way to other frameworks like Angular.

lgas|2 months ago

Do you want monads? Because this is how you get monads.

ivan7237d|2 months ago

Author here. What's pretty cool about LazyPromise is that when it's paired with Signals, you can do some of the stuff that you'd otherwise do with functional reactive programming, but you don't have to tell your team to learn RxJS or another FRP library - instead there is an API that capitalizes on the existing knowledge of the native promise.

conartist6|2 months 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.

ivan7237d|2 months ago

Yes, do take a look, I've just added a `log` function that makes it easy to play around with lazy promises and see everything that's going on.

IntelliAvatar|2 months ago

Observability is step one. The hard part is what the system is allowed to do once you observe it.