admin管理员组文章数量:1023025
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');
});
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
1 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');
});
本文标签: javascriptHow can I run a function alongside an async oneStack Overflow
版权声明:本文标题:javascript - How can I run a function alongside an async one? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745566726a2156492.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
await
– Evert Commented Nov 19, 2024 at 17:05