简体   繁体   English

简单的node.js应用程序控制流程

[英]Simple node.js app control flow

I have easy noob question concerning control flow of basic app. 我对基本应用程序的控制流程有一个很简单的问题。 I have 3 functions with setTimeout inside. 我有3个带有setTimeout的函数。

console.log("[+] Program start");

function first(){
setTimeout(function(){
  console.log(1);
},3000);}

function second(){
setTimeout(function(){
  console.log(2);
},2000);}

function third(){
setTimeout(function(){
  console.log(3);  
},1000);}

first();
second();
third();

console.log("done");

Output is as expected this: 预期输出如下:

[+] Program start
done
3
2
1

I would like to control flow that I will see things in following order: 我想控制流程,以便按以下顺序查看内容:

[+] Program start
1
2
3
done

So I rewrote program by following way: 所以我通过以下方式重写了程序:

console.log("[+] Program start");

function first(){
setTimeout(function(){
  console.log(1);
  second();
},3000);}

function second(){
setTimeout(function(){
  console.log(2);
  third();
},2000);}

function third(){
setTimeout(function(){
  console.log(3);  
  call();
},1000);}

first();

function call(){console.log("done ");}

Output is: 输出为:

[+] Program start
1
2
3
done 

Now output is ok, I would like to ask you, is this approach right? 现在输出正常,我想问你,这种方法对吗? It this right way how to control flow or how to write in node.js? 用这种正确的方式如何控制流或如何在node.js中编写? Or I am totally on wrong way. 或者我完全走错了路。 Could you please check it and give me some hints, advices etc. Thank you very much for help. 您能否检查一下并给我一些提示,建议等。非常感谢您的帮助。

If you're trying to manage the order of operations, you definitely don't want to be using setTimeout() to do it. 如果您要管理操作的顺序,则绝对不希望使用setTimeout()来执行此操作。 setTimeout() is designed to block the thread from executing until done, and used this way would make your application extraordinarily fragile. setTimeout()用于阻止线程执行直到完成,并且使用这种方式会使您的应用程序异常脆弱。

Assuming you are just trying to manage the flow, read up on how Node.js's event loop works. 假设您只是尝试管理流程,请阅读Node.js的事件循环的工作原理。 Your code, written in that fashion, would look like this (I've added in some setTimeout() functions in there to simulate/illustrate long-running functions): 以这种方式编写的代码将如下所示(我在其中添加了一些setTimeout()函数以模拟/说明长时间运行的函数):

console.log("[+] Program start")

var first = function(callback) {
    setTimeout(function() {
        console.log(1)
        callback()
    }, 1000)
}

var second = function(callback) {
    setTimeout(function() {
        console.log(2)
        callback()
    }, 1000)
}

var third = function(callback) {
    setTimeout(function() {
        console.log(3)
        callback()
    }, 1000)
}


first(function() {
    second(function() {
        third(function() {
            console.log("done ")        
        })
    })
})

Having said that, there's an inherent downside to using callbacks in a nested fashion like this: "callback hell". 话虽如此,以这样的嵌套方式使用回调存在一个固有的缺点:“回调地狱”。 You may consider modularizing your functions, using something like async 's waterfall, or a Promise library instead. 您可以考虑使用async的瀑布之类的内容或Promise库来对函数进行模块化。

Bonus points: You could also write the function chain in ES6 quite a bit more concisely: 优点:您还可以在ES6中编写功能链更加简洁:

first(() => second(() => third(() =>
    console.log("done ")        
)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM