并行動態獲取請求

我想知道如何并行執行動態獲取請求,我已經嘗試了12個小時了,但我想不出答案,我到處查看了google和StackOverflow,我真的很累。


      for (const fileindex of filelist) {
        let dcmFilename = `slice_${fileindex}.dcm`
        slices.push( {index:fileindex, filename:dcmFilename} )
          fd.append(dcmFilename, files[fileindex], dcmFilename);
        fd.append('slices', JSON.stringify(slices));
        loopcount += 1
        if (filecount == 24 || loopcount == filelist.length){
          
      if (cursorPos !== false) {
        let scaleFactor = Tegaki.scaleFactor;
        cursorPos.x = parseInt(cursorPos.x / scaleFactor);
        cursorPos.y = parseInt(cursorPos.y / scaleFactor);
        fd.append('x', cursorPos.x);
        fd.append('y', cursorPos.y)
      }

      // Get layer id from index
      if (index !== -1) {
        type = this.layerTypes[index]['id']
      }
      // switch mode from heuristic to pytorch vertebrae for vertebral bone
      if (type === 'vertebral-bone' ){
        mode='PyTorch-Vertebrae'
      }

      // Post to endpoint
      let domain = window.location.origin.replace(':8080', ':5000')
      let list = await fetch(`${domain}/segment?mode=${mode}&type=${type}`,  {    method: 'POST', body: fd   })
      let result = await list.json()
      // do something with result
      // finish then continue the loop and create new body and send a new request
     // clear formdata and continue loop
     fd =  new FormData()
}

我發送了下面的fetch請求,其中為每個請求生成主體,主體從來都不是同一個主體,主體由函數動態生成。是否有一種方法可以一次發送所有請求,然后等待它們返回響應,然后繼續我的其余代碼?

? 最佳回答:

您可以將filelist.map()async回調一起使用,而不是包含awaitfor/of循環。

由于.map()只是盲目地迭代數組而不等待任何返回的承諾,因此它將從.map()調用的所有async回調返回一個承諾數組。這些承諾最初將無法實現,所有fetch()操作將同時為"in-flight。然后,您可以在返回的承諾數組上使用await Promise.all(...),以了解它們何時完成:

await Promise.all(filelist.map(async fileindex => {
    let dcmFilename = `slice_${fileindex}.dcm`
    slices.push({ index: fileindex, filename: dcmFilename })
    fd.append(dcmFilename, files[fileindex], dcmFilename);
    fd.append('slices', JSON.stringify(slices));
    loopcount += 1
    if (filecount == 24 || loopcount == filelist.length) {

        if (cursorPos !== false) {
            let scaleFactor = Tegaki.scaleFactor;
            cursorPos.x = parseInt(cursorPos.x / scaleFactor);
            cursorPos.y = parseInt(cursorPos.y / scaleFactor);
            fd.append('x', cursorPos.x);
            fd.append('y', cursorPos.y)
        }

        // Get layer id from index
        if (index !== -1) {
            type = this.layerTypes[index]['id']
        }
        // switch mode from heuristic to pytorch vertebrae for vertebral bone
        if (type === 'vertebral-bone') {
            mode = 'PyTorch-Vertebrae'
        }

        // Post to endpoint
        let domain = window.location.origin.replace(':8080', ':5000')

        let list = await fetch(`${domain}/segment?mode=${mode}&type=${type}`, { method: 'POST', body: fd })
        let result = await list.json()
        // do something with result
        // finish then continue the loop and create new body and send a new request
    }
}));

注意#1:由于您現在并行運行多個fetch操作,因此任何處理其結果的代碼都不能共享變量。這段代碼中有幾個變量沒有顯示它們的聲明。這些聲明可能應該在這個循環中,帶有letconst,因此循環的每個迭代都有一個新的變量,并且您不會在迭代之間共享變量,除非該變量顯式地應該在迭代之間累積。此循環中沒有本地聲明的可疑變量可能會受到并行操作的影響,包括filecountloopcountcursorPosindexmodefdtype。您沒有在此處顯示整個執行上下文,因此我們無法看到足夠的內容來對這些內容提出完整的建議。

注意#2:包含此if (filecount == 24 || loopcount == filelist.length)的代碼看起來可能容易出現問題。如果您試圖在所有迭代都完成時運行一些代碼,那么在Promise.all()之后運行該代碼比嘗試檢測上一次迭代何時完成要好。記住,您的迭代現在不一定按順序進行,因為您正在并行運行它們。它們將按順序啟動,但不一定按順序完成。

主站蜘蛛池模板: 中文日韩字幕一区在线观看| 福利在线一区二区| 国模无码视频一区二区三区| 丝袜人妻一区二区三区网站| 在线精品一区二区三区电影| 国产成人精品日本亚洲专一区| 色婷婷AV一区二区三区浪潮 | 国产午夜精品一区二区三区| 国产av夜夜欢一区二区三区| 国产精品乱码一区二区三| 亚洲欧美日韩一区二区三区在线 | 日韩人妻不卡一区二区三区| 又硬又粗又大一区二区三区视频| 亚洲毛片不卡av在线播放一区| 国产精品综合一区二区| 日美欧韩一区二去三区| 日本v片免费一区二区三区 | 中文字幕一区二区三区久久网站| 日韩精品无码一区二区三区AV | 亚洲乱色熟女一区二区三区蜜臀| 日韩精品无码一区二区中文字幕| 国产精品日本一区二区在线播放 | 亚洲AV无码一区东京热久久| 国产一区二区三区久久精品| 国产一区二区三区不卡观| 亚洲AV本道一区二区三区四区| 色窝窝无码一区二区三区成人网站| 无码人妻精品一区二区| 3d动漫精品啪啪一区二区中| 91国偷自产一区二区三区| 亚洲码欧美码一区二区三区| 国产精品538一区二区在线| 一区二区三区观看| 另类一区二区三区| 亚洲无圣光一区二区| 少妇人妻精品一区二区| 国产激情无码一区二区三区| 久久久无码精品国产一区| 无码精品尤物一区二区三区| 国产伦理一区二区三区| 日韩精品无码视频一区二区蜜桃|