srmagura / real-cancellable-promise

Cancellable promise library for JavaScript and TypeScript.
https://srmagura.github.io/real-cancellable-promise
MIT License
33 stars 2 forks source link

Convenience API: pseudoCancellable #6

Closed adam-rocska closed 1 year ago

adam-rocska commented 1 year ago

Feature Value

At the time of writing the aforementioned function's contract is distributed as follows:

export declare function pseudoCancellable<T>(promise: PromiseLike<T>): CancellablePromise<T>;

For the world's most simplistic use cases (like writing tests) yields the following simplest usage:

   const {result, rerender, waitForNextUpdate} = renderHook(() =>
    useTaskQueue({
      name: 'test',
      codec: json.number,
      task: v => pseudoCancellable(Promise.resolve([v * 2])),
    })
  );

It would be nice to extend the API by providing an overload which supports the following usage:

   const {result, rerender, waitForNextUpdate} = renderHook(() =>
    useTaskQueue({
      name: 'test',
      codec: json.number,
      task: v => pseudoCancellable([v * 2]),
    })
  );
adam-rocska commented 1 year ago

Note, that this is a feature request I'm also more than happy to add if a core maintainer gives me the green light for a PR.

srmagura commented 1 year ago

Hi @adam-rocska, you can also write

CancellablePromise.resolve([v * 2])

which should have nearly the same behavior as pseudoCancellable(Promise.resolve([v * 2])). Does that address your concern?

adam-rocska commented 1 year ago

Sure, it should do the trick. This is just a convenience idea ¯_(ツ)_/¯

If it doesn't fit the lib', I'm cool with it.

srmagura commented 1 year ago

Yeah, I'm hesitant to introduce new APIs when it can already be accomplished with an existing function. You can always define your own function if this is a common pattern in your codebase 😃