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;