I'm having a problem with passing data from one API into another. It works but only after I submit it twice. Perhaps I don't understand Async/await well? Below is the code:
async function mainOne(e) {
e.preventDefault();
try {
const movieFetch = await pullMovie();
const earningFetch = await convertEarnings(movieFetch);
} catch(err){
console.log(err)
}
Here are the two functions:
const pullMovie = async () => {
let apiKey = '******'; //Omitted but it does function//
let title1 = movieOne.replaceAll(' ', '+')
let urlOne = `http://www.omdbapi.com/?t=${title1}&apikey=${apiKey}`
const response = await fetch(urlOne);
const data = await response.json();
setMovieOneInfo({
title: data.Title,
year: data.Year,
poster: data.Poster,
earnings: data.BoxOffice
})
return movieOneInfo; }
Here's the second:
const convertEarnings = async (movie) => {
let startingAmount = Number(movie.earnings.replace('$', '').replaceAll(',', ''));
let startingYear = Number(movie.year);
let url= `https://inflation-api.herokuapp.com/api/?value=${startingAmount}&year=${startingYear}`
let response = await fetch(url);
let data = await response.json();
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
})
const formatAdjEarnings = formatter.format(data.response.adjustedValue)
setMovieOneAdjEarnings(formatAdjEarnings)
return movieOneAdjEarnings;
}
The first function (pullMovie) feeds the component prop, but if I try to console.log in the mainOne function, it won't display correctly. Can someone please help? Thanks!