I want to kick off an async task but then have another function run that shows some animation.
I can't do
const myFunc = async () => {
// await asyncFunc()
// await animateFunc()
}
as it won't get to animateFunc
until asyncFunc
has resolved
I'm not sure I can wait on promises to resolve iether as animateFunc
is a continuous cyclical for loop that I run
I also want to try and avoid state with useEffect as that feels more like an anti pattern. I would like to call them both onClick and then break from the for loop once the async one resolves. what are my options here?
I want to kick off an async task but then have another function run that shows some animation.
I can't do
const myFunc = async () => {
// await asyncFunc()
// await animateFunc()
}
as it won't get to animateFunc
until asyncFunc
has resolved
I'm not sure I can wait on promises to resolve iether as animateFunc
is a continuous cyclical for loop that I run
I also want to try and avoid state with useEffect as that feels more like an anti pattern. I would like to call them both onClick and then break from the for loop once the async one resolves. what are my options here?
Share Improve this question edited Jan 10 at 16:39 Borewit 9551 silver badge20 bronze badges asked Nov 19, 2024 at 10:58 Red BaronRed Baron 7,70112 gold badges47 silver badges112 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 1Execute both asyncFunc()
and animateFunc()
in parallel:
const doBothAtOnce = () => {
return Promise.all([asyncFunc(), animateFunc()]);
};
await doBothAtOnce();
Demo:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function asyncFunc(id) {
for (let n = 0; n < 4; ++n) {
console.log(`Function ${id}, iteration ${n}`);
await sleep(5);
}
}
const runBoth = () => Promise.all([asyncFunc(1), asyncFunc(2)]);
runBoth().then(() => {
console.log('Completed');
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745566728a4633437.html
await
– Evert Commented Nov 19, 2024 at 17:05