@alanscodelog/utils
    Preparing search index...

    Function defer

    • Wraps the given function in a defered promise, such that it will not execute until the promise is externally resolved. If you reject it, you must catch it.

      const deferred = defer(async (message) => {
      console.log(message)
      })

      // nothing happens at this point
      deferred()
      .then(() => console.log("after"))
      .catch(() => console.log("catches deferred.reject"))

      deferred.resolve("message") // function logs
      // or
      deferred.reject() // above catch catches rejection

      Type Parameters

      Parameters

      • func: T

      Returns T & {
          reject: (...args: any[]) => void;
          resolve: (...args: Parameters<T>[0]) => void;
      }