josdejong/workerpool

`exec` generic type def doubly wrapped `Promise`?

Open

#485 建立於 2025年1月29日

在 GitHub 查看
 (4 留言) (0 反應) (0 負責人)JavaScript (164 fork)github user discovery
enhancementhelp wanted

倉庫指標

Star
 (2,297 star)
PR 合併指標
 (PR 指標待抓取)

描述

Currently the type definition for exec looks like this:

exec<T extends (...args: any[]) => any>(method: string | T, params?: Parameters<T> | null | undefined, options?: import("./types.js").ExecOptions | undefined): Promise<ReturnType<T>>;

Note the Promise<ReturnType<T>>. I think this unnecessarily wraps the ReturnType in a Promise when the return type already is a promise. I think it should be something like Promisify<ReturnType<T>> and type Promisify<T> = T extends Promise<any> ? T : Promise<T>; (wraps type in promise, but only if not already a promise).

I pretty sure it should be like this, because just testing out...

// worker

export const encode = async (blah: any) => {
  const encoder = await createEncoder();
  // ...
  return new Blob();
};

export type Encode = typeof encode;

workerpool.worker({ encode });

// consumer

import EncodeWorker from "./encode.worker?worker&url";
import { Encode } from "./encode.worker.ts";

const encoderPool = workerpool.pool(EncodeWorker);
const getBlob = (blah: any) =>
  encoderPool.exec<Encode>("encode", [blah]);

// test

getBlob(blah).then((result) => console.log(result));

Typescript says result is type Promise<Blob>, but console log shows just Blob. I didn't look at the exec implementation, but the result is definitely just a blob, so the typing must be wrong.

貢獻者指南