javascript - How can I run a function alongside an async one? - Stack Overflow

I want to kick off an async task but then have another function run that shows some animation.I can�

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
  • 2 developer.mozilla./en-US/docs/Web/JavaScript/Reference/… – mousetail Commented Nov 19, 2024 at 11:00
  • didn't realise each promise in promise.all could be done like that, should have read docs more. thanks! – Red Baron Commented Nov 19, 2024 at 11:07
  • although i'm not sure the promise is resolving now – Red Baron Commented Nov 19, 2024 at 11:16
  • 1 I'm not sure what you mean. Maybe post a new question with a minimal example of a continuous for loop and promises that explains exactly what you want and what you have and the difference between them – mousetail Commented Nov 19, 2024 at 11:45
  • 1 At the risk of stating the obvious, if you don't want to wait for a function to complete you can also just not await – Evert Commented Nov 19, 2024 at 17:05
 |  Show 3 more comments

1 Answer 1

Reset to default 1

Execute 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信