getify/TNG-Hooks

Feature: `usePromisify(..)`

オープン

#16 opened on 2019/01/14

 (6 件のコメント) (0 件のリアクション) (0 人の担当者)JavaScript (43 件のフォーク)batch import
enhancementhelp wanted

Repository metrics

Stars
 (1,014 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

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.

コントリビューターガイド