而循环不等待等待

发布于 2025-01-23 10:19:27 字数 618 浏览 2 评论 0原文

我知道有很多这样的问题,但是我找不到适合我的情况的问题。我正在循环循环拨打API,只是不想快速开火来超时我的电话。 While循环似乎并没有等待通话完成。

while(it < array.length){
    await apiFunction(array[it]);
    console.log(it);
    it++
}

功能

async apiFunction(array){
    try{
        (async () => {
            const result = await api.doStuff(array);
            console.dir(result);
        }
    } catch (e) {
        stuff;
    }
}

而且我也尝试添加返回的

(结果),但这并没有更改我的控制台输出的任何内容,

0
1
result
result

我该如何才能使way循环等待直到API调用完成?所以我的控制台看起来像

0
result
1
result

I know there's a ton of these questions but I haven't been able to find one that fits my case. I'm making api calls in a while loop and just don't want to time out my calls by rapid firing them. The while loop doesn't seem to be waiting for the call to finish.

while(it < array.length){
    await apiFunction(array[it]);
    console.log(it);
    it++
}

and the function

async apiFunction(array){
    try{
        (async () => {
            const result = await api.doStuff(array);
            console.dir(result);
        }
    } catch (e) {
        stuff;
    }
}

I've also tried adding a return(result) but that didn't change anything

My console output is

0
1
result
result

How can I get the while loop to wait until the api call is done? so my console looks like

0
result
1
result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2025-01-30 10:19:27

我想我有太多的异步性。刚刚从呼吸中删除异步

async apiFunction(array){
    try{
        const result = await api.doStuff(array);
        console.dir(result);
    } catch (e) {
        stuff;
    }
}

I had too many layers of asynchronicity going I guess. Just removed the async from inside the apiFunction

async apiFunction(array){
    try{
        const result = await api.doStuff(array);
        console.dir(result);
    } catch (e) {
        stuff;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文