简体   繁体   English

如何在Node.js中处理异步函数

[英]How to handle async functions in nodejs

I have been learning nodejs, and one thing that continues to boggle my mind is async programming that nodejs is built on. 我一直在学习Node.js,而我的脑海中一直困扰着我的是基于Node.js的异步编程。 Maybe I am doing something wrong, but despite all my research I can't wrap my mind around how I should adapt to it coming from synchronous programming background. 也许我做错了事,但是尽管我做了所有的研究,但我仍无法确定应该如何适应来自同步编程背景的技术。 I would really appreciate if someone gave me a simple async example based on the below structure, given you want to call func2 from anywhere. 如果有人想从任何地方调用func2,那么如果有人给我一个基于以下结构的简单异步示例,我将不胜感激。

function1(){
  var obj = function2();
  console.log(obj); //"undefined".
}

function2(){
  //do stuff with DB and get obj
  console.log(obj); //logs obj.
  return obj;
}

The problem I stumbled upon is that obj prints as undefined in func1, while func2 has no problems with it. 我偶然发现的问题是obj在func1中打印为未定义,而func2对此没有任何问题。 When I nested the functions within each other it worked, leading me to believe that due to async nature of nodejs it proceeded to log in func1 before func2 was finished. 当我将这些函数相互嵌套时,它起作用了,使我相信由于nodejs的异步特性,它在func2完成之前便开始登录func1。 Am I wrong with my assumptions? 我的假设错了吗?

What I struggle with in the above example, is how would I code the functions instead of nesting them within each other, so I can call func2 from several different functions at once. 在上面的示例中,我遇到的困难是如何对函数进行编码,而不是将它们相互嵌套,因此我可以一次从几个不同的函数调用func2。 I've looked into callbacks but couldn't understand the examples given in various answers with a function inside a function. 我已经研究了回调,但无法理解函数内部带有函数的各种答案中给出的示例。

You have to define functions (ie the callbacks) within functions. 您必须在函数内定义函数(即回调)。

1  function function1(){
2    function2(function(obj) {
3      console.log(obj); //logs obj.
     });
   }

4  function function2(callbackFn){
     //do stuff with DB and get obj
     console.log(obj); //logs obj.
5    callbackFn(obj);
   }

function2 can be called from many places at once, not just function1 . function2可以从很多地方在被调用一次,不只是function1

If you can be more specific about what you don't understand, then you might get a better answer. 如果您可以更具体地了解不了解的内容,那么您可能会得到更好的答案。


Functions are values, just like numbers, strings and objects. 函数是值,就像数字,字符串和对象一样。

This is the core fundamental of functional programming, and probably the thing you haven't grasped. 这是函数式编程的核心基础,而且可能是您尚未掌握的内容。 In JavaScript, as in a number of languages, functions are not special*. 与许多语言一样,在JavaScript中,功能不是特殊的*。 This is different to languages like C or Java, where functions/methods are different to the usual values you deal with in your code. 这不同于C或Java等语言,在C / Java中,函数/方法与您在代码中处理的常规值不同。

2; // Here is a number
var a = 2; // I can assign it to a variable
console.log(2); // I can pass it as a function argument

function() {} // Here is a function
var a = function() {}; // I can assign it to a variable
console.log(function() {}); // I can pass it as a function argument

function jim() {} // This function's name is jim
var a = jim; // I can assign it to a variable
console.log(jim); // I can pass it as a function argument

(function() {})(); // I can call a function without giving it any names
jim(); // I can call a named function
a(); // I can call a function via a variable
jim.call(this, arg1); // Or I can get complicated

So, we can declare functions and pass them around, separately to calling them. 因此,我们可以分别声明函数并将其传递给调用它们。 Now, what about the code we started with. 现在,我们开始的代码呢?

  1. Declare a new function called function1 that takes no arguments 声明一个不带参数的名为function1的新函数
  2. When function1 is called, call function2 with a single argument, a new nameless function that takes an obj argument function1被调用,调用function2有一个参数,一个新的无名函数,它的obj参数
  3. When the anonymous function is called, call console.log with the value passed for obj 调用匿名函数时,使用传递给obj的值调用console.log
  4. Declare a new function called function2 that takes a single argument callbackFn 声明一个名为function2的新函数,该function2接受单个参数callbackFn
  5. Call the value passed for callbackFn with a single argument, the obj we got from somewhere 用一个参数调用为callbackFn传递的值,即我们从某处获得的obj

So the declaration order is 1, 4, 2; 因此,声明顺序为1、4、2; and the execution order is 2, 5, 3. 并且执行顺序为2、5、3。

*OK, they're a little special. *好的,他们有点特别。 Their definitions are hoisted , you can execute the code they represent, and their declarations create scope and closures . 它们的定义被提升 ,您可以执行它们所代表的代码,并且它们的声明创建作用域闭包 But those are all questions for another time. 但是这些都是另一个问题。

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

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