我怎样才能每分钟发出10条承诺?
我在列表中有这些动态日期:
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"];
我想获取与这些日期相关的内容或结果。但是我不想一气呵成,但是每分钟都要记下10件,直到日期不复存在。
这是我的工作代码:
var promises = dates.map(function (date, i) {
return getContent(date);
});
Promise.all(promises).then((data) => {
console.log(data);
}).catch((err) => {
console.log(err.message);
});
它连续获取内容,而不是我想要的内容。
但是有可能实现我的需要吗?有什么想法吗?
备注:
getContent(date)
包含大量的async await
承诺,因此调用它的promse.all
也必须是promise
,否则我可以获得所有返回数据当承诺结束时,在一个地方(console.log(data);
)。
修改
const delayValue = (val, ms) => {
new Promise(resolve => {
setTimeout(resolve.bind(null, val), ms)
});
}
答案 0 :(得分:1)
如果您坚持使用promises,请使用设置的超时解析器创建一个承诺。
在内部弹出数组并做任何你喜欢的事情。承诺所有这些承诺都会成为我认为的伎俩。
但为什么要承诺而不是设置超时的简单for循环?
答案 1 :(得分:1)
Promise只会解析一次,所以如果你需要每分钟都有结果,和想用promises做这件事,你需要为每个数据块分别做一个承诺。
以下是通过使用自定义delayValue
函数重复创建承诺来实现此目的的方法:
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
现在要将getContent
因素添加到其中,您可以像执行map
一样(虽然可以缩短语法),然后在每个块上应用Promise.all
:< / p>
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;