亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在抓取內部使用抓取不是執行所有抓取請求

在抓取內部使用抓取不是執行所有抓取請求

米琪卡哇伊 2022-08-27 10:52:22
我正在嘗試逐個執行三個獲取請求。每個抓取請求應在完成上一個抓取請求時觸發。以下是我的代碼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”被執行?我做錯了什么?
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

你不能在循環中做抓取。您將返回完成的第一個抓取。使用 promise 或 await/async 在循環中獲取。如何在循環中返回許多承諾,并等待它們都做其他事情


查看完整回答
反對 回復 2022-08-27
?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

我寧愿這樣做,創建一個IIFE,并為后續的獲取請求遞歸調用它:


return dispatch =>{

    var ctr = 0;

    (function myFunc(url, headerObj){

        fetch(url, headerObj)

        .then(response => {

            response.json().then(data=>{

                ctr++;

                if(ctr ===1 ){ // This could be any condition, say, something on the basis of response; I have taken `ctr` as a condition

                    myFunc(url, { //You may change this even to different URL, if needed

                        method: 'POST',

                        headers: {

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

                             'body': ...,

                             'Authorization':...


                          }

                    });

                }else if(ctr === 2){

                    myFunc(url, {

                        method: 'POST',

                        headers: {

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

                             'body': ...,

                             'Authorization':...

                          }

                    });

                }else{

                   // Any other code

                }


            })

        })

    })(url, headerObj);

}


查看完整回答
反對 回復 2022-08-27
  • 2 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號