Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

326
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda