josdejong/workerpool

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

Open

#485 aperta il 29 gen 2025

Vedi su GitHub
 (4 commenti) (0 reazioni) (0 assegnatari)JavaScript (164 fork)github user discovery
enhancementhelp wanted

Metriche repository

Star
 (2297 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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.

Guida contributor