Skip to content

Instantly share code, notes, and snippets.

@g4rcez
Created June 9, 2020 17:28
Show Gist options
  • Save g4rcez/47d3ec51cd2e3f6f7f72059bb76c1053 to your computer and use it in GitHub Desktop.
Save g4rcez/47d3ec51cd2e3f6f7f72059bb76c1053 to your computer and use it in GitHub Desktop.
interface PromiseLike<T> {
then<R1 = T, R2 = never>(
resolve?: ((value: T) => R1 | PromiseLike<R1>) | undefined | null,
reject?: ((reason: any) => R2 | PromiseLike<R2>) | undefined | null
): PromiseLike<R1 | R2>;
}
type ReadonlyPromise<T> = PromiseLike<Readonly<T>> | Readonly<T>;
const PromiseAll = async <T>(...values: ReadonlyPromise<T>[]): Promise<T> => {
const promises = await Promise.all(values[0] as any);
return promises as any;
};
@g4rcez
Copy link
Author

g4rcez commented Jun 9, 2020

Fix in promise array

interface PromiseLike<T> {
  then<R1 = T, R2 = never>(
    resolve?: ((value: T) => R1) | undefined | null,
    reject?: ((reason: any) => R2) | undefined | null
  ): PromiseLike<R1 | R2>;
}

type ReadonlyPromise<T> = Readonly<T>;

type Then<T> = T extends Promise<infer U> ? U :
  T extends ((...args: any[]) => Promise<infer V>) ? V : T 

type AP<T> = Array<T extends Promise<infer U> ? PromiseLike<U> : T>

const PromiseAll = async <T>(...values: ReadonlyPromise<AP<T>>[]): Promise<Then<T>> => {
  const promises = await Promise.all(values[0] as any);
  return promises as any;
};

const promise = new Promise<string>(res => res("ok"))

const a = [ promise ] as const;

PromiseAll(a).then(e => {
    const f = e[0]
    console.log(f)
}) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment