srmagura/real-cancellable-promise

Convenience API: pseudoCancellable

Closed this issue · 4 comments

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]),
    })
  );
  • or similar

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.

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?

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

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

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 😃