这里是一个完整的nodejs初学者并且学习异步编程 - 特别是承诺。但我完全不知道如何将像for-loop这样的同步操作“转换”为异步调用。
var loopfunction = function(version:string) : void {
console.log("Begin" + version + " Loop")
for (let i = 0; i <= 10; i++) {
console.log(version + "Number looping through is " + i)
}
console.log("End Loop")
}
console.log("Calling sync function")
loopfunction("First : ");
loopfunction("Second : ");
loopfunction("Third : ");
console.log("ending sync function")
在上面的代码中,'loopfunction'调用是同步执行的。执行“First”调用,然后执行第二次调用,然后执行第三次调用。
如何以异步方式编写异步函数/代码来执行相同的循环函数调用?即。第二个和第三个循环函数调用不等待先前的循环函数调用完成并同时运行。
非常感谢
答案 0 :(得分:0)
如果你想要一个纯粹的异步调用,它可能看起来像:
var loopfunction = function(version:string) : void {
setTimeout(function(){
console.log("Begin" + version + " Loop")
for (let i = 0; i <= 10; i++) {
console.log(version + "Number looping through is " + i)
}
console.log("End Loop")
}, 0);
}
console.log("Calling sync function")
loopfunction("First : ");
loopfunction("Second : ");
loopfunction("Third : ");
console.log("ending sync function")
如果您正在考虑创建并返回承诺:
var loopfunction = function(version:string) {
return new Promise(function(resolve){
console.log("Begin" + version + " Loop")
for (let i = 0; i <= 10; i++) {
console.log(version + "Number looping through is " + i)
}
console.log("End Loop")
resolve();
});
}
console.log("Calling sync function")
loopfunction("First : ");
loopfunction("Second : ");
loopfunction("Third : ");
console.log("ending sync function")
然而,在这种情况下,你不是在等待承诺。如果你想等待(所以在&#34; sync&#34;方式中执行promises),那么:
var loopfunction = function(version:string) {
return new Promise(function(resolve){
console.log("Begin" + version + " Loop")
for (let i = 0; i <= 10; i++) {
console.log(version + "Number looping through is " + i)
}
console.log("End Loop")
resolve();
});
}
console.log("Calling sync function")
loopfunction("First : ").then(function(){
return loopfunction("Second : ");
}). then(function(){
return loopfunction("Third : ");
}). then(function(){
console.log("ending sync function")
});
如果您想查看不同的结果顺序,那么您可以尝试这样做:
var loopfunction = function(version:string) {
return new Promise(function(resolve) {
//wait for some random time before executing the code
setTimeout(function(){
console.log("Begin" + version + " Loop")
for (let i = 0; i <= 10; i++) {
console.log(version + "Number looping through is " + i)
}
console.log("End Loop")
resolve();
}, Math.floor((Math.random() * 1000) + 1));
});
}
答案 1 :(得分:0)
如果您希望长时间运行的代码不阻止,只需将其更改为
var loopfunction = function(version:string) : void {
console.log("Begin" + version + " Loop")
for (let i = 0; i <= 10; i++) {
setImmediate(function() {
console.log(version + "Number looping through is " + i)
})
}
console.log("End Loop")
}
如果您还希望它返回结果或知道它何时完成,请将代码包装在promises中:
var loopfunction = function(version:string) : void {
console.log("Begin" + version + " Loop")
var iterations = []
for (let i = 0; i <= 10; i++) {
iterations.push(new Promise(function(resolve) {
setImmediate(function() {
console.log(version + "Number looping through is " + i)
resolve()
})
}))
}
return Promise.all(iterations).then(function() {
console.log("End Loop")
})
}