在抓取内部使用抓取不是执行所有抓取请求

我正在尝试逐个执行三个获取请求。每个抓取请求应在完成上一个抓取请求时触发。以下是我的代码


const chopSegment = (token, frame_tag_url, tag_to_delete_id, chopped_tag_array, tags_for_index_update) => (dispatch) =>  {

    let req = fetch(frame_tag_url + tag_to_delete_id + "/",

        {

            method: "DELETE",

            headers: {

                "Authorization": "Token " + token,

                "content-type": "application/json"

            }

        })

    req.then(response => {

        if (!response.ok) {

            throw response;

        }

        else

            return response.json();

    }).then(response => {

        return fetch(frame_tag_url,

            {

                method: "POST",

                headers: {

                    "Authorization": "Token " + token,

                    "content-type": "application/json",

                },

                body : JSON.stringify(tags_for_index_update)

            }).then(response1 => {

            if (!response1.ok) {

                throw response1;

            }

            return response1.json();

        }).then(response => {

            for(let i = 0; i < chopped_tag_array.length; i++){

                return  fetch(frame_tag_url,

                    {

                        method: "POST",

                        body: JSON.stringify(chopped_tag_array[i]),

                        headers: {

                            "Authorization": "Token " + token,

                            "content-type": "application/json"

                        }

                    })

                .then(response2 => {

                    if (!response2.ok) {

                        throw response2;

                    }

                    return response2.json();

                }).then(response2 => {

                    dispatch(chopSegmentSuccess(response2))

                }).catch(error => {


                })

            }

        }).catch(error => {


        })

    }).catch(error => {

    })

}

在我的代码中,只有第一次抓取,即“DELETE”被执行?我做错了什么?


米琪卡哇伊
浏览 75回答 2
2回答

aluckdog

你不能在循环中做抓取。您将返回完成的第一个抓取。使用 promise 或 await/async 在循环中获取。如何在循环中返回许多承诺,并等待它们都做其他事情

慕丝7291255

我宁愿这样做,创建一个IIFE,并为后续的获取请求递归调用它:return dispatch =>{&nbsp; &nbsp; var ctr = 0;&nbsp; &nbsp; (function myFunc(url, headerObj){&nbsp; &nbsp; &nbsp; &nbsp; fetch(url, headerObj)&nbsp; &nbsp; &nbsp; &nbsp; .then(response => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.json().then(data=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctr++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(ctr ===1 ){ // This could be any condition, say, something on the basis of response; I have taken `ctr` as a condition&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFunc(url, { //You may change this even to different URL, if needed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'content-type': 'application/json',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'body': ...,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Authorization':...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else if(ctr === 2){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFunc(url, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'content-type': 'application/json',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'body': ...,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Authorization':...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Any other code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; })(url, headerObj);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript