¿Cuál debería ser el tipo de retorno de Promise.race? (Typescript)
Por ejemplo, en el código a continuación, ¿cuál debería ser el tipo de response? Typescript sigue dándome errores como, Type 'unknown' is not assignable to type 'Promise'
const response: [what type???] = await Promise.race([
apicall(),
timeoutPromise(),
]);
Respuesta #1
TypeScript no puede estar seguro de qué promesa se resolverá primero o incluso si alguna de ellas se resolverá en absoluto. Por lo tanto, deberías proporcionar un tipo a Promise.race:
type PromiseType1 = string
type PromiseType2 = string
const apicall = () => new Promise<PromiseType1>(() => {})
const timeoutPromise = () => new Promise<PromiseType2>(() => {})
const response = await Promise.race<PromiseType1 | PromiseType2>([
apicall(),
timeoutPromise(),
]);
