JavaScript - 每分钟发送10个承诺?

时间:2021-02-25 01:09:18

How can I dispatch 10 promises in every minute?

我怎样才能在每分钟发出10个承诺?

I have these dynamic dates in a list:

我在列表中有这些动态日期:

var dates = ["2016-08-31T23:00:00.000Z","2016-09-01T23:00:00.000Z","2016-09-02T23:00:00.000Z","2016-09-03T23:00:00.000Z","2016-09-04T23:00:00.000Z","2016-09-05T23:00:00.000Z","2016-09-06T23:00:00.000Z","2016-09-07T23:00:00.000Z","2016-09-08T23:00:00.000Z","2016-09-09T23:00:00.000Z","2016-09-10T23:00:00.000Z","2016-09-11T23:00:00.000Z","2016-09-12T23:00:00.000Z","2016-09-13T23:00:00.000Z","2016-09-14T23:00:00.000Z","2016-09-15T23:00:00.000Z","2016-09-16T23:00:00.000Z","2016-09-17T23:00:00.000Z","2016-09-18T23:00:00.000Z","2016-09-19T23:00:00.000Z","2016-09-20T23:00:00.000Z","2016-09-21T23:00:00.000Z","2016-09-22T23:00:00.000Z","2016-09-23T23:00:00.000Z","2016-09-24T23:00:00.000Z","2016-09-25T23:00:00.000Z","2016-09-26T23:00:00.000Z","2016-09-27T23:00:00.000Z","2016-09-28T23:00:00.000Z","2016-09-29T23:00:00.000Z","2016-09-30T23:00:00.000Z","2016-10-01T23:00:00.000Z"];

I want to get the content or result that associated with these dates. But I don't want to get them in one go, but every 10 items in every minute, until the date is no more.

我想获得与这些日期相关的内容或结果。但是我不想一气呵成,而是每分钟都有10件物品,直到日期不复存在。

This is my working code:

这是我的工作代码:

     var promises = dates.map(function (date, i) {
        return getContent(date);
    });

    Promise.all(promises).then((data) => {
        console.log(data);
    }).catch((err) => {
        console.log(err.message);
    });

It gets the content in a row, not exactly what I want.

它连续获取内容,而不是我想要的内容。

But is it possible to achieve what I need? Any ideas?

但是有可能实现我的需要吗?有任何想法吗?

Notes:

笔记:

getContent(date) contains a massive of async await promises, so in that promse.all that calls it has to be a promise too i guess, otherwise I can get all return data in one place (which is console.log(data);) when the promises end.

getContent(date)包含大量的async await promises,所以在promse.all中调用它必须是一个承诺我猜,否则我可以在一个地方获取所有返回数据(这是console.log(数据);当承诺结束时。

EDIT:

编辑:

const delayValue = (val, ms) => {
    new Promise(resolve => {
        setTimeout(resolve.bind(null, val), ms)
    });
}

2 个解决方案

#1


1  

A Promise only resolves once, so if you need to have results every minute, and want to do this with promises, you'll need to have a separate promise per chunk of data.

Promise只解析一次,所以如果你需要每分钟都有结果,并希望用promises做到这一点,你需要为每个数据块分别设置一个promise。

Here is how you could do that by repeatedly creating a promise with a custom delayValue function:

以下是通过使用自定义delayValue函数重复创建promise的方法:

const delayValue = (val, ms) => 
    new Promise(resolve => setTimeout(resolve.bind(null, val), ms));

async function loopDates(dates, chunk, delay) {
    for (let i = 0; i < dates.length; i += chunk) {
        console.log(await delayValue(dates.slice(i, i+chunk), i ? delay : 0));
    }
}

var dates = ["2016-08-31T23:00:00.000Z", "2016-09-01T23:00:00.000Z", "2016-09-02T23:00:00.000Z", "2016-09-03T23:00:00.000Z", "2016-09-04T23:00:00.000Z", "2016-09-05T23:00:00.000Z", "2016-09-06T23:00:00.000Z", "2016-09-07T23:00:00.000Z", "2016-09-08T23:00:00.000Z", "2016-09-09T23:00:00.000Z", "2016-09-10T23:00:00.000Z", "2016-09-11T23:00:00.000Z", "2016-09-12T23:00:00.000Z", "2016-09-13T23:00:00.000Z", "2016-09-14T23:00:00.000Z", "2016-09-15T23:00:00.000Z", "2016-09-16T23:00:00.000Z", "2016-09-17T23:00:00.000Z", "2016-09-18T23:00:00.000Z", "2016-09-19T23:00:00.000Z", "2016-09-20T23:00:00.000Z", "2016-09-21T23:00:00.000Z", "2016-09-22T23:00:00.000Z", "2016-09-23T23:00:00.000Z", "2016-09-24T23:00:00.000Z", "2016-09-25T23:00:00.000Z", "2016-09-26T23:00:00.000Z", "2016-09-27T23:00:00.000Z", "2016-09-28T23:00:00.000Z", "2016-09-29T23:00:00.000Z", "2016-09-30T23:00:00.000Z", "2016-10-01T23:00:00.000Z"];

loopDates(dates, 10, 2000);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Now to add the getContent factor into it, you would do the map like you did (although the syntax can be shortened), and then apply Promise.all on each chunk:

现在要将getContent因子添加到其中,你可以像你一样做地图(虽然可以缩短语法),然后在每个块上应用Promise.all:

const delayValue = (val, ms) => 
    new Promise(resolve => setTimeout(resolve.bind(null, val), ms));

// Mock for getContent:
const getContent = date => Promise.resolve(date);
    
async function loopDates(dates, chunk, delay) {
    const promises = dates.map(getContent);
    
    for (let i = 0; i < dates.length; i += chunk) {
        console.log(await delayValue(Promise.all(promises.slice(i, i+chunk)), 
                                     i ? delay : 0));
    }
}

var dates = ["2016-08-31T23:00:00.000Z", "2016-09-01T23:00:00.000Z", "2016-09-02T23:00:00.000Z", "2016-09-03T23:00:00.000Z", "2016-09-04T23:00:00.000Z", "2016-09-05T23:00:00.000Z", "2016-09-06T23:00:00.000Z", "2016-09-07T23:00:00.000Z", "2016-09-08T23:00:00.000Z", "2016-09-09T23:00:00.000Z", "2016-09-10T23:00:00.000Z", "2016-09-11T23:00:00.000Z", "2016-09-12T23:00:00.000Z", "2016-09-13T23:00:00.000Z", "2016-09-14T23:00:00.000Z", "2016-09-15T23:00:00.000Z", "2016-09-16T23:00:00.000Z", "2016-09-17T23:00:00.000Z", "2016-09-18T23:00:00.000Z", "2016-09-19T23:00:00.000Z", "2016-09-20T23:00:00.000Z", "2016-09-21T23:00:00.000Z", "2016-09-22T23:00:00.000Z", "2016-09-23T23:00:00.000Z", "2016-09-24T23:00:00.000Z", "2016-09-25T23:00:00.000Z", "2016-09-26T23:00:00.000Z", "2016-09-27T23:00:00.000Z", "2016-09-28T23:00:00.000Z", "2016-09-29T23:00:00.000Z", "2016-09-30T23:00:00.000Z", "2016-10-01T23:00:00.000Z"];

loopDates(dates, 10, 2000);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#2


1  

If you are insisting on using promises then, create a promise with a set timeout resolver.

如果您坚持使用promises,请使用设置的超时解析器创建一个promise。

Inside pop the array and do whatever you like. Promise all of an array of those promises would do the trick I think.

在弹出数组并做任何你喜欢的事情。承诺所有这些承诺中的所有承诺都会成为我认为的伎俩。

But why promises rather than a simple for loop with set timeout?

但为什么要承诺而不是设置超时的简单for循环?

#1


1  

A Promise only resolves once, so if you need to have results every minute, and want to do this with promises, you'll need to have a separate promise per chunk of data.

Promise只解析一次,所以如果你需要每分钟都有结果,并希望用promises做到这一点,你需要为每个数据块分别设置一个promise。

Here is how you could do that by repeatedly creating a promise with a custom delayValue function:

以下是通过使用自定义delayValue函数重复创建promise的方法:

const delayValue = (val, ms) => 
    new Promise(resolve => setTimeout(resolve.bind(null, val), ms));

async function loopDates(dates, chunk, delay) {
    for (let i = 0; i < dates.length; i += chunk) {
        console.log(await delayValue(dates.slice(i, i+chunk), i ? delay : 0));
    }
}

var dates = ["2016-08-31T23:00:00.000Z", "2016-09-01T23:00:00.000Z", "2016-09-02T23:00:00.000Z", "2016-09-03T23:00:00.000Z", "2016-09-04T23:00:00.000Z", "2016-09-05T23:00:00.000Z", "2016-09-06T23:00:00.000Z", "2016-09-07T23:00:00.000Z", "2016-09-08T23:00:00.000Z", "2016-09-09T23:00:00.000Z", "2016-09-10T23:00:00.000Z", "2016-09-11T23:00:00.000Z", "2016-09-12T23:00:00.000Z", "2016-09-13T23:00:00.000Z", "2016-09-14T23:00:00.000Z", "2016-09-15T23:00:00.000Z", "2016-09-16T23:00:00.000Z", "2016-09-17T23:00:00.000Z", "2016-09-18T23:00:00.000Z", "2016-09-19T23:00:00.000Z", "2016-09-20T23:00:00.000Z", "2016-09-21T23:00:00.000Z", "2016-09-22T23:00:00.000Z", "2016-09-23T23:00:00.000Z", "2016-09-24T23:00:00.000Z", "2016-09-25T23:00:00.000Z", "2016-09-26T23:00:00.000Z", "2016-09-27T23:00:00.000Z", "2016-09-28T23:00:00.000Z", "2016-09-29T23:00:00.000Z", "2016-09-30T23:00:00.000Z", "2016-10-01T23:00:00.000Z"];

loopDates(dates, 10, 2000);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Now to add the getContent factor into it, you would do the map like you did (although the syntax can be shortened), and then apply Promise.all on each chunk:

现在要将getContent因子添加到其中,你可以像你一样做地图(虽然可以缩短语法),然后在每个块上应用Promise.all:

const delayValue = (val, ms) => 
    new Promise(resolve => setTimeout(resolve.bind(null, val), ms));

// Mock for getContent:
const getContent = date => Promise.resolve(date);
    
async function loopDates(dates, chunk, delay) {
    const promises = dates.map(getContent);
    
    for (let i = 0; i < dates.length; i += chunk) {
        console.log(await delayValue(Promise.all(promises.slice(i, i+chunk)), 
                                     i ? delay : 0));
    }
}

var dates = ["2016-08-31T23:00:00.000Z", "2016-09-01T23:00:00.000Z", "2016-09-02T23:00:00.000Z", "2016-09-03T23:00:00.000Z", "2016-09-04T23:00:00.000Z", "2016-09-05T23:00:00.000Z", "2016-09-06T23:00:00.000Z", "2016-09-07T23:00:00.000Z", "2016-09-08T23:00:00.000Z", "2016-09-09T23:00:00.000Z", "2016-09-10T23:00:00.000Z", "2016-09-11T23:00:00.000Z", "2016-09-12T23:00:00.000Z", "2016-09-13T23:00:00.000Z", "2016-09-14T23:00:00.000Z", "2016-09-15T23:00:00.000Z", "2016-09-16T23:00:00.000Z", "2016-09-17T23:00:00.000Z", "2016-09-18T23:00:00.000Z", "2016-09-19T23:00:00.000Z", "2016-09-20T23:00:00.000Z", "2016-09-21T23:00:00.000Z", "2016-09-22T23:00:00.000Z", "2016-09-23T23:00:00.000Z", "2016-09-24T23:00:00.000Z", "2016-09-25T23:00:00.000Z", "2016-09-26T23:00:00.000Z", "2016-09-27T23:00:00.000Z", "2016-09-28T23:00:00.000Z", "2016-09-29T23:00:00.000Z", "2016-09-30T23:00:00.000Z", "2016-10-01T23:00:00.000Z"];

loopDates(dates, 10, 2000);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#2


1  

If you are insisting on using promises then, create a promise with a set timeout resolver.

如果您坚持使用promises,请使用设置的超时解析器创建一个promise。

Inside pop the array and do whatever you like. Promise all of an array of those promises would do the trick I think.

在弹出数组并做任何你喜欢的事情。承诺所有这些承诺中的所有承诺都会成为我认为的伎俩。

But why promises rather than a simple for loop with set timeout?

但为什么要承诺而不是设置超时的简单for循环?