Javascript 如何在使用fetch时动态循环for循环中的多个承诺?

Javascript 如何在使用fetch时动态循环for循环中的多个承诺?,javascript,node.js,for-loop,ecmascript-6,fetch-api,Javascript,Node.js,For Loop,Ecmascript 6,Fetch Api,这就是工作原理: const limit = 1000 // fetchMyProducts(page, limit, flag) return fetchMyProducts(1, 1, true) .then(function (products) { return fetchMyProducts(2, limit, false) }).then(function (totalProducts) { return fet

这就是工作原理:

    const limit = 1000
    // fetchMyProducts(page, limit, flag)
    return fetchMyProducts(1, 1, true)
    .then(function (products) {
        return fetchMyProducts(2, limit, false)
    }).then(function (totalProducts) {
        return fetchMyProducts(3, limit, false)
    }).then(function (totalProducts) {
        return fetchMyProducts(4, limit, false)
    }).then(function (totalProducts) {
        return fetchMyProducts(5, limit, false)
    }).then(function (totalProducts) {
        return fetchMyProducts(6, limit, false)
    }).then(function (totalProducts) {
        return fetchMyProducts(7, limit, false)
    })
我正试图通过fetch获取系统中的所有产品。 问题是,目前我知道有多少产品,但在1年/3年内。。。谁知道

我正在尝试动态地循环一个fetch并获取所有的产品

我已经试过了,但是它似乎根本没有被调用

    return fetchMyProducts(1, 1, true)
    .then(function (numberOfProducts) {
        let pages = Math.ceil(numberOfProducts / 1000) + 1;
        console.log(pages);
        return getAllProducts = () => {
            for (let i = 1; i < pages; i++) {
                const element = array[i];
                return fetchMyProducts(2, limit, false)

            }
        }
    }).then(... something else)
返回fetchMyProducts(1,1,true)
.然后(功能(产品编号){
设pages=Math.ceil(numberOfProducts/1000)+1;
控制台日志(页);
return getAllProducts=()=>{
for(设i=1;i
是否有一种方法可以循环获取承诺并在完成时返回某些内容,然后继续执行其他操作?

您正在寻找的

const limit = 1000
let chain = Promise.resolve();
for (let i=1; i<8; i++) {
    chain = chain.then(function(products) {
        return fetchMyProducts(i, limit, false)
    });
}
return chain;

一个人不会在for循环中返回
,并期望所述循环继续。我不明白,如果你想得到所有产品,为什么不创建一个api并调用它?为什么要提出很多要求才能得到所有的产品呢?第一次,第一个很好地工作了!你是一个传奇。非常感谢你。
const limit = 1000
for (let i=1; i<8; i++) {
    const products = await fetchMyProducts(i, limit, false);
}
return;