josdejong / josdejong/workerpool

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

Open
#485 4 comments 0 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
JavaScript
Stars
2.3k
Forks
164
PR merge metrics
No merged PRs in 30d

Description

Currently the type definition for `exec` looks like this:

```ts
exec any>(method: string | T, params?: Parameters | null | undefined, options?: import("./types.js").ExecOptions | undefined): Promise>;
```

Note the `Promise>`. 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>` and `type Promisify = T extends Promise ? T : Promise;` (wraps type in promise, but only if not already a promise).

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

```ts
// 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", [blah]);

// test

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

Typescript says `result` is type `Promise`, 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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.