1 回答
TA贡献1786条经验 获得超13个赞
您应该“承诺”事件发射器对象(something.data.on),然后您可以等待结果:
const itVolOne = async () => {
if (boolpool) {
const pres = mvc.Components.get("search1");
const alldata3 = await new Promise(resolve => {
pres.data('results', {count: 0, output_mode: 'json_rows'}).on("data",
results => {
resolve(results._data);
}
);
});
console.log(JSON.stringify(alldata3));
const rdbms1 = mvc.Components.get("search2");
const alldata32 = await new Promise(resolve => {
rdbms1.data('results', {count: 0, output_mode: 'json_rows'}).on("data",
results => {
resolve(results._data);
}
);
});
console.log(JSON.stringify(alldata32));
return { alldata3, alldata32 }
} else {
throw new Error('Unable to get the datas');
}
}
因此,现在 itVolOne 函数以“同步方式”返回所有结果,其余函数可以重写:
const itVolTwo = alldata3 => {
return alldata3.rows.reduce((rdbmsData, row) => {
rdbmsData[row[0]] = {
"crit": row[1],
"high": row[2],
"med": row[3],
"low": row[4]
};
return rdbmsData;
}, {});
}
const itVolThree = alldata32 => {
return alldata32.rows.reduce((presData, row) => {
presData[row[0]] = {
"crit": row[1],
"high": row[2],
"med": row[3],
"low": row[4]
};
return presData;
}, {});
}
const itVolFour = (rdbmsData, presData) => {
function sum(a, b) {
Object.keys(b).forEach(k => {
if (b[k] && typeof b[k] === 'object') return sum(a[k] = a[k] || {}, b[k]);
a[k] = (+a[k] || 0) + +b[k];
});
return a;
}
return [rdbmsData, presData].reduce(sum);
}
现在您只能在第一步中等待:
async function itInit() {
try {
console.log('I got to the call YEY!');
const { alldata3, alldata32 } = await itVolOne();
const stepTwo = itVolTwo(alldata3);
const stepThree = itVolThree(alldata32);
const endData = itVolFour(stepTwo, stepThree);
console.log(JSON.stringify(endData));
console.log('Done');
}
catch (error) {
console.log('u done f\'ed up');
}
}
并调用主函数:
itInit();
就这样。
添加回答
举报