首先需要引入async库
npm i async之后使用async库中的async.map
它会将每次循环的结果暂存在callback中,并在最后形成结果集返回
const async = require('async');
let signArr = [];
let signData = new Promise((resolve, reject) => {
async.map(signDataArr, function (data, callback) {
models.signUp.count({ unit_name: data }, function (error, datas) {
if (error) {
callback(error, 1);
} else {
let signJson = { name: data, count: datas };
signArr.push(signJson);
callback(null, 1);
}
});
}, function (err, result) { err ? reject('err') : resolve(result); });
});
await signData;