type-fest#PromiseValue TypeScript Examples

The following examples show how to use type-fest#PromiseValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PromiseUtil.ts    From joplin-blog with MIT License 6 votes vote down vote up
/**
   * 创建一个支持 on* 事件的 Promise 实例
   * @param executor
   */
  static warpOnEvent<
    F extends (events: any) => Promise<any>,
    E extends Parameters<F>[0],
    K extends ConditionalKeys<E, VoidFunc>
  >(executor: F): OnEventPromise<PromiseValue<ReturnType<F>>, E> {
    const events: Partial<Pick<E, K>> = {}
    const res = new Promise(async (resolve, reject) => {
      await wait(0)
      try {
        resolve(await executor(events))
      } catch (e) {
        reject(e)
      }
    })
    Reflect.set(res, 'on', (type: K, callback: E[K]) => {
      events[type] = callback
      return res
    })
    return res as any
  }
Example #2
Source File: PromiseUtil.ts    From joplin-utils with MIT License 6 votes vote down vote up
/**
   * 创建一个支持 on* 事件的 Promise 实例
   * @param executor
   */
  static warpOnEvent<
    // eslint-disable-next-line
    F extends (events: any) => Promise<any>,
    E extends Parameters<F>[0],
    K extends ConditionalKeys<E, VoidFunc>,
  >(executor: F): OnEventPromise<PromiseValue<ReturnType<F>>, E> {
    const events: Partial<Pick<E, K>> = {}
    const res = wait(0).then(
      () =>
        new Promise((resolve, reject) => {
          try {
            resolve(executor(events))
          } catch (e) {
            reject(e)
          }
        }),
    )
    Reflect.set(res, 'on', (type: K, callback: E[K]) => {
      events[type] = callback
      return res
    })
    return res as OnEventPromise<PromiseValue<ReturnType<F>>, E>
  }