繁体   English   中英

我应该使用Promise.All而不是async.each吗?

[英]Should I use Promise.All instead of async.each?

当前使用async.each构建数组。 我想知道是否有使用es6的替代方法? 我应该使用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;
}

用法:

await this.pushDataArray(response);

不,您既不要使用它们。 您的代码中没有异步的。 写吧

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;
}

甚至更简单

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM