如何将参数传递到那时

发布于 2025-02-13 03:05:52 字数 768 浏览 2 评论 0原文

当调用下面的功能时,我将不确定Q。我不知道该怎么办。请帮忙。谢谢。我尝试将Q作为参数传递到当时,但它不起作用。

myfunction = () =>{
      
       sendFetch(
               `http://api.com`,
               GET
             ).then((response)=>{
              
               for (var i = 0; i < response.data.length; i++) {
                 processed_data.push(response.data[i].description);
               }
               
               for (var q = 0 ; q < array.length; q++)
               {
                sendFetch(
                  `api.com`,
                  GET
                ).then((response)=>{

                  
                console.log(q)
                 
                })

               }
              
               
             })
        
   } 

When call the function below I am getting undefined for q. I do not know what to do. Please help. Thank you. I have tried passing q into then as a parameter but it does not work.

myfunction = () =>{
      
       sendFetch(
               `http://api.com`,
               GET
             ).then((response)=>{
              
               for (var i = 0; i < response.data.length; i++) {
                 processed_data.push(response.data[i].description);
               }
               
               for (var q = 0 ; q < array.length; q++)
               {
                sendFetch(
                  `api.com`,
                  GET
                ).then((response)=>{

                  
                console.log(q)
                 
                })

               }
              
               
             })
        
   } 

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

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

发布评论

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

评论(2

叫思念不要吵 2025-02-20 03:05:52

我建议研究范围和“ var”和“ let”之间的差异。
也有异步和承诺。

我们不再使用“ var” s,并且要谨慎声明具有等效名称的变量。您正在同一范围中使用“响应”。

I suggest studying scopes and the difference between "var" and "let".
Also async and promises.

We don't use "var"s anymore and be careful about declaring variables with equals name. You are using "response" in the same scope.

初相遇 2025-02-20 03:05:52

您可以等待sendfetch而不是使用然后捕获。
尝试一下:

myfunction = () => {

    sendFetch(
        `http://api.com`,
        GET
    ).then(async (response) => {
        for (var i = 0; i < response.data.length; i++) {
            processed_data.push(response.data[i].description);
        }

        for (var q = 0; q < array.length; q++) {
            try {
                const res = await sendFetch(
                    `api.com`,
                    GET
                )
                console.log(res)
                console.log(q)
            } catch (error) {
                console.log(error.message)
            }
        }
    }).catch(error => {
        console.log(error.message)
    })

} 

You can await the sendFetch instead of using then catch.
Trye this:

myfunction = () => {

    sendFetch(
        `http://api.com`,
        GET
    ).then(async (response) => {
        for (var i = 0; i < response.data.length; i++) {
            processed_data.push(response.data[i].description);
        }

        for (var q = 0; q < array.length; q++) {
            try {
                const res = await sendFetch(
                    `api.com`,
                    GET
                )
                console.log(res)
                console.log(q)
            } catch (error) {
                console.log(error.message)
            }
        }
    }).catch(error => {
        console.log(error.message)
    })

} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文