Is there anyway to delay the return of a function using setTimeout()
?
function foo(){
window.setTimeout(function(){
//do something
}, 500);
//return "something but wait till setTimeout() finishes";
}
Using promises:
const fetchData = () =>
new Promise(resolve => {
setTimeout(() => resolve(apiCall()), 3000);
});
Answer updated thanks to @NikKyriakides who pointed out async/await is not necessary. I initially had async () => resolve(await apiCall())
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With