TNG-Hooks icon indicating copy to clipboard operation
TNG-Hooks copied to clipboard

Feature: `usePromisify(..)`

Open getify opened this issue 6 years ago • 6 comments

The usePromisify(..) hook would wrap/lift a callback-expecting function as a promise-returning function. It would expect callback-last, and err-first-callback.

Similar to Node's util.promisify(..), this hook would allow overriding of that assumption via a Symbol added to the function in question, to define its own promisified version. In that case, the usePromisify(..) hook would just return that value and do nothing else.

The benefits of this as a hook:

  1. Since it would store the promisified version in a state slot, it allows you to promisify inline function expressions safely without unnecessary work (the wrapping would only happen the first time).

    Just like useMemo(..) and useEffect(..), we'd allow optional guards to override and re-define, if closure was required.

  2. It doesn't require polluting the outer/global namespace with a promisified version of the function. In other words, it allows you to only locally create a promisified override without affecting the rest of the program.

getify avatar Jan 14 '19 15:01 getify

I would love to help on this if possible I just might need a bit of guidance!

sbaidon avatar Jan 31 '19 16:01 sbaidon

@sbaidon great, thanks! happy to have the help!

I want to hold off on adding hooks briefly, while I work on the re-org in #20. But hopefully this won't take too long to finish and unblock future hooks work. :)

getify avatar Jan 31 '19 16:01 getify

Got it 👍

sbaidon avatar Feb 05 '19 16:02 sbaidon

hoping to give this some work this week. :)

getify avatar Feb 05 '19 20:02 getify

@getify, Can I take this up?

phenax avatar Oct 02 '19 14:10 phenax

I got stuck on #20 (mostly design choice wise) after getting quite a bit of work in on it. You can see that work in progress on this branch: https://github.com/getify/TNG-Hooks/tree/issue-20-externalize-context

But because of the nature of that change, it would invalidate any of these other features, so I wanted to wait on them for #20 to be finished. I do hope to get back to that work and finish it soon, but I have to work out some details of design first.

getify avatar Oct 02 '19 14:10 getify