function resolveToCallback(fun) {
  return function (...args) {
    const maybeCallback = args[args.length - 1];
    if (typeof maybeCallback === "function") {
      const fulfilled = maybeCallback.bind(null, null);
      const rejected = maybeCallback.bind(null);
      fun.apply(this, args.slice(0, -1)).then(fulfilled, rejected);
    } else {
      return fun.apply(this, args);
    }
  };
}
Callbackifyable wrapper for async functions

@template T, Args
@param {(...args: Args) => Promise<T>} fun
@returns {<CBArgs = [...Args, (err: any, value: T) => void], InnerArgs extends Args | CBArgs>(...innerArgs: InnerArgs) => InnerArgs extends CBArgs ? void : Promise<T>}

@example
const fn = resolveToCallback(async () => { return 42; })
// with callback:
fn((err, value) => { ... })
// with await:
const value = await fn()