I am still fairly new to working with async and await functions, and even newer to implementing promise functions. I am trying to print out whatever my asynchronous function returns. When I log the output to the console, it always shows me the correct output, but when I try to save it and return it, it only works every other go. I have this similar code in react and it seems to work fine on the first go there, but with NextJs I am running into the issue that it does not run on the first go but second. I am attaching my code and would appreciate a general direction for where I can go to solve this issue.
import React, {useState} from 'react'
import { getStockPrice } from '../configs/stockapi';
function HomeInvestment() {
const [entered, setEntered] = useState(false);
const [ticker, setTicker] = useState('');
const [price, setPrice] = useState(0);
const [checks, setChecks] = useState([])
const clear = () => {
setEntered(false);
setChecks([])
}
const handleSubmit = async() => {
const idT = document.getElementById('t');
console.log(ticker);
const p = stockPrice(ticker).then(result => {
let data = {
ticker: ticker,
price: result
}
setChecks([...checks, data])
setPrice(p);
})
setEntered(true);
console.log(checks);
idT.value = null
}
const stockPrice = async(ticker) => {
return new Promise((resolve, reject) => {
getStockPrice(ticker)
.then(result => {
console.log(result)
resolve(result.toFixed(2))
})
.catch(error => {
reject(error)
})
})
}
return (
<div className='flex justify-center'>
<div className='w-full max-w-[40ch] bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4'>
<div className='flex text-black justify-center flex-col bg-slate-300 px-2 py-3 rounded mb-6'>
<div className='mb-4'>
<label className="block text-gray-700 text-sm font-bold mb-2 text-center" >
Enter Stock Ticker
</label>
<input id= 't' required className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" type="text" placeholder="Stock Ticker" onChange={(e) => setTicker(e.target.value)}/>
</div>
<button className='bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full focus:outline-none focus:shadow-outline mx-10 ' type="button" onClick={handleSubmit}>
Check Stock Info
</button>
</div>
{entered &&
<div>
<div className=''>
<button onClick={clear} className="text-red-600">Clear</button>
</div>
<div className='flex text-black justify-center flex-col bg-slate-300 px-2 py-3 rounded'>
{checks.forEach((element) => {
return (
<div>
<p>{element.ticker}</p>
<p>{element.price}</p>
</div>
)
})}
{price}
</div>
</div>
}
</div>
</div>
)
}
export default HomeInvestment
asyncsyntax to always use that, and not use the old.then()etc Promise API. That's what theasyncsyntax features are meant to simplify.asyncwithawait? codesandbox.io/s/xenodochial-feather-t2heml?file=/src/… Can you replicate your code here?const stockPrice = async (ticker) => { return getStockPrice(ticker) .then((result) => { console.log(result); }) .catch((error) => {}); };Orconst stockPrice = async (ticker) => { return await getStockPrice(ticker) };