Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
Where does this requirement for a Promise<void> come from?

I have a helper function that uses fetch to get data from an API:

const callApi = (endpoint: string, method = 'GET', body?: string): Promise<Response> => {
  // decide if url is dev or prod
  const url = getUrl()
  const headers = new Headers()
  headers.set('infinote-token', store.infinoteToken as string)
  const initObject = {
    headers: headers,
    method: method,
    body: method === 'POST' ? body : undefined
  }
  return fetch(`${url}${endpoint}`, initObject)
    .then(r => {
      if (r.ok) {
        apiIsOnline()
      } else if (r.status === 403) {
        store.needToSetToken = true
        apiIsOnline()
      } else {
        apiIsOffline()
        throw Error(r.statusText)
      }
    })
    .catch((reason) => {
      log.debug(`API call rejected because: ${reason}`)
      return Promise.reject()
    })
}

It generates a TS error:

TS2322: Type 'Promise<void | Response>' is not assignable to type 'Promise<Response>'.    
          Type 'void | Response' is not assignable to type 'Response'.
            Type 'void' is not assignable to type 'Response'.

Where does this requirement for Promise<void> come from?

My understanding is that fetch returns a Promise<Response>:

The promise resolves to the Response object representing the response to your request.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yeah, fetch returns a Promise<Response>. But you are handling this result with .then(r => ...) This r is the Response you get back from fetch. The overall result of a promise chain foo().then().then()... is the result of the last then(). But your then() handler doesn't return anything (well technically it does return a Promise<void>). Thus the overall result of your fetch(...).then(...) is a Promise<void>. If you just want to pass through the Response, add a return r to your then()

return fetch(`${url}${endpoint}`, initObject)
  .then(r => {
    if (r.ok) {
      apiIsOnline()
    } else if (r.status === 403) {
      store.needToSetToken = true
      apiIsOnline()
    } else {
      apiIsOffline()
      throw Error(r.statusText)
    }
    return r;
  })
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda