[英]Should I use Promise.All instead of async.each?
Currently using async.each to build array. 当前使用async.each构建数组。 I wonder if there is alternative approach using es6? 我想知道是否有使用es6的替代方法? Should I use Promise.All? 我应该使用Promise.All吗?
async pushDataArray(response) {
if (response && response.data && response.data.length > 0) {
async.each(response.data, (data, error) => {
if (this.messagesData.length < this.maxCount) {
this.messagesData.push(data);
}
});
}
return this.messagesData;
}
Usage: 用法:
await this.pushDataArray(response);
No, you should not use neither. 不,您既不要使用它们。 There is nothing asynchronous in your code. 您的代码中没有异步的。 Just write 写吧
pushDataArray(response) {
if (response && response.data) {
for (const data of response.data) {
if (this.messagesData.length < this.maxCount) {
this.messagesData.push(data);
}
});
}
return this.messagesData;
}
or even simpler 甚至更简单
pushDataArray(response) {
if (response && response.data) {
this.messagesData.push(...response.data.slice(0, this.maxCount - this.messagesData.length));
}
return this.messagesData;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.