第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

遞歸循環(huán)一個承諾?

遞歸循環(huán)一個承諾?

森林海 2021-12-23 16:50:21
我從 DigitalOcean API 獲得了一個液滴列表,但液滴列表是每頁的。響應(yīng)為您提供頁面上和下一頁上的液滴列表...我試圖遞歸地獲取每個承諾的下一頁:getDropletsPerPage(command,firstPage).then((response)=>{    nextPage= response['nextPage']    droplets= response['droplets']    getDropletsPerPage(command, nextPage).then((response)=>{        nextPage= response['nextPage']        droplets= response['droplets']        getDropletsPerPage(command, nextPage).then((response)=>{            nextPage= response['nextPage']            droplets= response['droplets']            // Repeat until last page..        })    })})
查看完整描述

3 回答

?
繁花不似錦

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個贊

你可以使用遞歸:


const dispatcher = {

    page: firstPage,

    droplets: [],

    execute: function () {

        const self = this;

        return new Promise(function (resolve, reject) {

            getDropletsPerPage(command, this.page).then(function (response) {

                self.page = response['nextPage'];

                self.droplets = self.droplets.concat(response['droplets']);

                if (nextPage === LAST_PAGE) {

                    resolve(true);/* done */

                } else {

                    self.execute().then(function () {

                        resolve(true);

                    });

                }

            });

        });

    }

}

dispatcher.execute().then(function() {

    /* reached last page */

});


查看完整回答
反對 回復(fù) 2021-12-23
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個贊

一個簡單的方法來做到這一點(diǎn):


    async getDropletsList(command, dropletsList, currentPage) {

        if(!currentPage) return dropletsList // here you've to check if there is another valid page

        const { nextPage, droplets } = await getDropletsPerPage(command, currentPage)

        dropletsList = dropletsList.concat(droplets)

        return getDropletsList(command, dropletsList, nextPage) // recursive call

    }

希望對你有幫助:D


查看完整回答
反對 回復(fù) 2021-12-23
?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個贊

如果您的目標(biāo)是從所有頁面收集液滴信息,您可以將數(shù)據(jù)累積在一個數(shù)組中,該數(shù)組作為參數(shù)傳遞給遞歸函數(shù)的每個后續(xù)調(diào)用(直到到達(dá)最后一頁):


const getDroplets = (page, droplets = []) => {

  if (/* end of the pagination is reached */) {

    return Promise.resolve(droplets);

  }

  return getDropletsPerPage(command, page).then((response) => {

    return getDroplets(

      response.nextPage,

      droplets.concat(...response.droplets)

    );

  })

};


getDroplets(firstPage).then(droplets => {

  console.log(droplets);

});


查看完整回答
反對 回復(fù) 2021-12-23
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號