Promise在Node.js嵌套异步中不起作用
我在Node.js中编写了许多基准脚本,用于Redis模式的不同设计。我试图按顺序运行所有基准测试功能。我在StackOverflow上遇到了许多异步模板来解决此问题。这些模板在简单的异步上可以正常工作,但是我的单个基准测试函数包含许多嵌套的异步函数,当我使用这些模板时,嵌套的异步函数按顺序运行,但整个基准并行运行。这是示例:
单个基准测试结果如下所示:single benchmark
[当我使用Promise模板运行所有基准测试时:many benchmark
基准功能看起来像。其他具有相同的结构:
async function CMbenchmark(redis,size,flag){
var time_stamp0;
var time_stamp1;
var time_stamp2;
redis.flushall().then(()=>{
//initialize data
time_stamp0 = performance.now();
console.log("benchmark size: "+size);
CMinitializeFakeData(redis,size)
}).then(()=>{
time_stamp1 = performance.now();
console.log("time for initialize data: "+size);
console.log(time_stamp1-time_stamp0);
searchEntryByBM(redis,"gender","male");
}).then(()=>{
time_stamp2 = performance.now();
console.log("time for search operation: ");
console.log(time_stamp2-time_stamp1);
redis.memory("stats").then(function(result){
console.log(result[0]+": "+result[1]/1000000+"MB");
console.log(result[2]+": "+result[3]/1000000+"MB");
});
});
}
我尝试过的一些执行功能,许多其他变化看起来很相似:
async function benchAll(){
Promise.resolve(benchmark2(redis,size,0)).then(function(){
return Promise.resolve(benchmark(redis,size,0));
}).then(function(){
return Promise.resolve(CMbenchmark(redis,size,0));
});
}
async function benchAll2(){
const result1 = await benchmark(redis,size,0);
const result2 = await benchmark2(redis,size,0);
const result3 = await CMbenchmark(redis,size,0);
}
指:How should I call 3 functions in order to execute them one after the other?。回调链也不起作用。除了寻找解决方案。我也在寻找有关为什么回调链可用于简单异步但不能嵌套异步的解释。
回答如下:您需要return
您想要在下一个.then()
中使用的承诺。
您非常亲密!每次调用.then(function(){...})
时,您都应该返回一些值,以便下一个.then()
会收到它。
redis.flushall().then(()=>{
//initialize data
time_stamp0 = performance.now();
console.log("benchmark size: "+size);
return CMinitializeFakeData(redis,size)// <-- added return here
}).then(()=>{
time_stamp1 = performance.now();
console.log("time for initialize data: "+size);
console.log(time_stamp1-time_stamp0);
return searchEntryByBM(redis,"gender","male"); // <-- added return here
}).then(()=>{
time_stamp2 = performance.now();
console.log("time for search operation: ");
console.log(time_stamp2-time_stamp1);
redis.memory("stats").then(function(result){
console.log(result[0]+": "+result[1]/1000000+"MB");
console.log(result[2]+": "+result[3]/1000000+"MB");
});
});
Promise在Node.js嵌套异步中不起作用
我在Node.js中编写了许多基准脚本,用于Redis模式的不同设计。我试图按顺序运行所有基准测试功能。我在StackOverflow上遇到了许多异步模板来解决此问题。这些模板在简单的异步上可以正常工作,但是我的单个基准测试函数包含许多嵌套的异步函数,当我使用这些模板时,嵌套的异步函数按顺序运行,但整个基准并行运行。这是示例:
单个基准测试结果如下所示:single benchmark
[当我使用Promise模板运行所有基准测试时:many benchmark
基准功能看起来像。其他具有相同的结构:
async function CMbenchmark(redis,size,flag){
var time_stamp0;
var time_stamp1;
var time_stamp2;
redis.flushall().then(()=>{
//initialize data
time_stamp0 = performance.now();
console.log("benchmark size: "+size);
CMinitializeFakeData(redis,size)
}).then(()=>{
time_stamp1 = performance.now();
console.log("time for initialize data: "+size);
console.log(time_stamp1-time_stamp0);
searchEntryByBM(redis,"gender","male");
}).then(()=>{
time_stamp2 = performance.now();
console.log("time for search operation: ");
console.log(time_stamp2-time_stamp1);
redis.memory("stats").then(function(result){
console.log(result[0]+": "+result[1]/1000000+"MB");
console.log(result[2]+": "+result[3]/1000000+"MB");
});
});
}
我尝试过的一些执行功能,许多其他变化看起来很相似:
async function benchAll(){
Promise.resolve(benchmark2(redis,size,0)).then(function(){
return Promise.resolve(benchmark(redis,size,0));
}).then(function(){
return Promise.resolve(CMbenchmark(redis,size,0));
});
}
async function benchAll2(){
const result1 = await benchmark(redis,size,0);
const result2 = await benchmark2(redis,size,0);
const result3 = await CMbenchmark(redis,size,0);
}
指:How should I call 3 functions in order to execute them one after the other?。回调链也不起作用。除了寻找解决方案。我也在寻找有关为什么回调链可用于简单异步但不能嵌套异步的解释。
回答如下:您需要return
您想要在下一个.then()
中使用的承诺。
您非常亲密!每次调用.then(function(){...})
时,您都应该返回一些值,以便下一个.then()
会收到它。
redis.flushall().then(()=>{
//initialize data
time_stamp0 = performance.now();
console.log("benchmark size: "+size);
return CMinitializeFakeData(redis,size)// <-- added return here
}).then(()=>{
time_stamp1 = performance.now();
console.log("time for initialize data: "+size);
console.log(time_stamp1-time_stamp0);
return searchEntryByBM(redis,"gender","male"); // <-- added return here
}).then(()=>{
time_stamp2 = performance.now();
console.log("time for search operation: ");
console.log(time_stamp2-time_stamp1);
redis.memory("stats").then(function(result){
console.log(result[0]+": "+result[1]/1000000+"MB");
console.log(result[2]+": "+result[3]/1000000+"MB");
});
});