我正在学习如何使用带有诺言的async / await。我对第一个承诺有一些疑问。具体来说:
var pres = mvc.Components.get("search1");
pres.data('results', {
count: 0,
output_mode: 'json_rows'
}).on("data", function(results) {
alldata3 = results._data;
console.log(alldata3)
});
我正在从另一个脚本中的函数提取结果。 “ search1”的结果非常大。例如,它需要更长的时间然后搜索两个。我遇到的问题是,它不会等到'results._data'完全分配给alldata3才能继续进行到itVolTwo()。最终给我一个异步错误,指示“ alldata3.rows”未定义。无论如何,是否有必要对其进行重新处理,以使其等待直到从search1接收到全部结果?最好的做法是什么?感谢您的帮助!
var alldata3 = new Object();
var alldata32 = new Object();
// Promise
const itVolOne = new Promise(
(resolve, reject) => {
if (boolpool) {
var pres = mvc.Components.get("search1");
pres.data('results', {
count: 0,
output_mode: 'json_rows'
}).on("data", function(results) {
alldata3 = results._data;
console.log(alldata3)
});
console.log(alldata3)
var rdbms1 = mvc.Components.get("search2");
rdbms1.data('results', {
count: 0,
output_mode: 'json_rows'
}).on("data", function(results) {
alldata32 = results._data;
console.log(alldata32)
});
console.log(alldata32)
console.log(alldata3, alldata32)
resolve(alldata3, alldata32)
} else {
const reason = new Error('Unable to get the datas');
reject(reason);
}
}
);
var rdbmsData = new Object();
async function itVolTwo() {
return new Promise(
(resolve, reject) => {
for await (let row of alldata3.rows) {
rdbmsData[row[0]] = {
"crit": row[1],
"high": row[2],
"med": row[3],
"low": row[4]
}
}
console.log(rdbmsData)
resolve(rdbmsData);
}
);
};
// 2nd promise
var presData = new Object();
async function itVolThree() {
return new Promise(
(resolve, reject) => {
for await (let row of alldata32.rows) {
presData[row[0]] = {
"crit": row[1],
"high": row[2],
"med": row[3],
"low": row[4]
}
}
console.log(presData)
resolve(presData);
}
);
};
// 3rd promise
var endData = new Object();
async function itVolFour() {
return new Promise(
(resolve, reject) => {
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;
}
endData = [rdbmsData, presData].reduce(sum);
console.log(endData)
resolve(endData);
}
);
};
// call our promise
async function itInit() {
try {
console.log('I got to the call YEY!');
let stepOne = await itVolOne;
let stepTwo = await itVolTwo();
let stepThree = await itVolThree();
let stepFour = await itVolFour();
console.log(endData);
console.log('Done');
} catch (error) {
console.log('u done f'
ed up ');
}
}
(async() => {
await itInit();
})();
答案 0 :(得分:1)
您应“保证”事件发射器对象(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();
仅此而已。