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

324
Vistas
Typescript: promise.then() still returning a promise

I have a function that returns a promise. I then want to get the value of said function, but when I call promise.then( result => result ) I'm still getting a promise.

The below code returns the following error:

error: Type 'Promise<void | SunriseSunsetResultsType>' is not assignable to type 'string'.

import {
    HebCalResponseType, 
    OpenweatherResponseType,
    SunriseSunsetResultsType,
    SunriseSunsetResponseType
} from "../src/app";

const lat: number = 40.8915;
const lng: number = -74.0119;
const date: string = "2012-11-09";
const url: string = `https://api.sunrise-sunset.org/json?lat=${lat}&lng=${lng}&date=${date}&formatted=0`;

async function request(url: string): Promise<HebCalResponseType | OpenweatherResponseType | SunriseSunsetResponseType> {
    // Make a request of one of our three external apis
    const jsonResult: (HebCalResponseType | OpenweatherResponseType | SunriseSunsetResponseType) = (await fetch(url)).json();
    return jsonResult;
};

const promise: Promise<HebCalResponseType | OpenweatherResponseType | SunriseSunsetResponseType> = request(url); 
const msg: Promise<SunriseSunsetResultsType | void> =  promise
    .then(response => { (response as SunriseSunsetResponseType).results; }, 
        error => { throw `failed to load ${url} with error ${error}`; })
const test: HTMLElement = document.getElementById("source");
test.innerText = msg;
about 4 years ago · Juan Pablo Isaza
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