Can async functions be called in onClick in button react
Your async function:
const asyncFunc = async () => {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("I am a done promise!"), 3000)
});
let result = await promise
alert(result);
}
Your button component call:
<button onClick={asyncFunc} >I am a button!</button>
This is how I call my asyncs inside an onClick
:
<Button onClick={async () => {await this.asyncFunc("Example");} }/>
async asyncFunc(text) {
console.log(text);
}