Tengo una función que devuelve una promesa. Luego quiero obtener el valor de dicha función, pero cuando llamo a promise.then (resultado => resultado) todavía obtengo una promesa.
El siguiente código devuelve el siguiente 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;