“Node.js:函数、回调函数(异步编程)”的版本间差异

来自Wikioe
跳到导航 跳到搜索
(建立内容为“category:Node.js == 关于 == '''Node.js 异步编程的直接体现就是回调'''。 * 异步编程依托于回调来实现,但不能说使用了回调…”的新页面)
 
无编辑摘要
 
(未显示同一用户的11个中间版本)
第1行: 第1行:
[[category:Node.js]]
[[category:Node.js教程]]


== 关于 ==
== 函数 ==
Node.js 中函数的使用与 JavaScript 类似,一个函数可以作为另一个函数的参数。
* 可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。
 
 
示例:
<syntaxhighlight lang="JavaScript" highlight="">
function say(word) {
  console.log(word);
}
 
function execute(someFunction, value) {
  someFunction(value);
}
 
execute(say, "Hello");
</syntaxhighlight>
 
=== 匿名函数 ===
匿名函数:可以直接在另一个函数的括号中定义和传递这个函数。
 
示例:
<syntaxhighlight lang="JavaScript" highlight="">
function execute(someFunction, value) {
  someFunction(value);
}
 
execute(function(word){ console.log(word) }, "Hello");
</syntaxhighlight>
 
=== 函数传递是如何让HTTP服务器工作的 ===
带着这些知识,我们再来看看我们简约而不简单的HTTP服务器:
<syntaxhighlight lang="JavaScript" highlight="">
var http = require("http");
 
http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);
</syntaxhighlight>
如上:向 createServer 函数传递了一个匿名函数。
 
 
用这样的代码也可以达到同样的目的:
<syntaxhighlight lang="JavaScript" highlight="">
var http = require("http");
 
function onRequest(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}
 
http.createServer(onRequest).listen(8888);
</syntaxhighlight>
 
== 回调函数(异步编程) ==
'''Node.js 异步编程的直接体现就是回调'''。
'''Node.js 异步编程的直接体现就是回调'''。
* 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。
* 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。
第9行: 第66行:
回调函数在完成任务后就会被调用;这样在执行代码时就没有阻塞或等待文件 I/O 操作。这就大大提高了 Node.js 的性能,可以处理大量的并发请求。
回调函数在完成任务后就会被调用;这样在执行代码时就没有阻塞或等待文件 I/O 操作。这就大大提高了 Node.js 的性能,可以处理大量的并发请求。
* 回调函数一般作为函数的最后一个参数出现。
* 回调函数一般作为函数的最后一个参数出现。
*: <syntaxhighlight lang="bash" highlight="">
*: <syntaxhighlight lang="JavaScript" highlight="">
function foo1(name, age, callback) { }
function foo1(name, age, callback) { }
function foo2(value, callback1, callback2) { }
function foo2(value, callback1, callback2) { }
第21行: 第78行:
</syntaxhighlight>
</syntaxhighlight>
# 创建 '''main.js''' 文件, 代码如下:
# 创建 '''main.js''' 文件, 代码如下:
#: <syntaxhighlight lang="bash" highlight="">
#: <syntaxhighlight lang="JavaScript" highlight="">
var fs = require("fs");
var fs = require("fs");


第44行: 第101行:
</syntaxhighlight>
</syntaxhighlight>
# 创建 '''main.js''' 文件, 代码如下:
# 创建 '''main.js''' 文件, 代码如下:
#: <syntaxhighlight lang="bash" highlight="">
#: <syntaxhighlight lang="JavaScript" highlight="">
var fs = require("fs");
var fs = require("fs");



2023年3月31日 (五) 21:21的最新版本


函数

Node.js 中函数的使用与 JavaScript 类似,一个函数可以作为另一个函数的参数。

  • 可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。


示例:

function say(word) {
  console.log(word);
}

function execute(someFunction, value) {
  someFunction(value);
}

execute(say, "Hello");

匿名函数

匿名函数:可以直接在另一个函数的括号中定义和传递这个函数。

示例:

function execute(someFunction, value) {
  someFunction(value);
}

execute(function(word){ console.log(word) }, "Hello");

函数传递是如何让HTTP服务器工作的

带着这些知识,我们再来看看我们简约而不简单的HTTP服务器:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

如上:向 createServer 函数传递了一个匿名函数。


用这样的代码也可以达到同样的目的:

var http = require("http");

function onRequest(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}

http.createServer(onRequest).listen(8888);

回调函数(异步编程)

Node.js 异步编程的直接体现就是回调

  • 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。
  • Node 使用了大量的回调函数,Node 所有 API 都支持回调函数


回调函数在完成任务后就会被调用;这样在执行代码时就没有阻塞或等待文件 I/O 操作。这就大大提高了 Node.js 的性能,可以处理大量的并发请求。

  • 回调函数一般作为函数的最后一个参数出现。
    function foo1(name, age, callback) { }
    function foo2(value, callback1, callback2) { }
    

阻塞代码

示例:

  1. 创建一个文件 input.txt ,内容如下:
    菜鸟教程官网地址:www.runoob.com
    
  2. 创建 main.js 文件, 代码如下:
    var fs = require("fs");
    
    var data = fs.readFileSync('input.txt');
    
    console.log(data.toString());
    console.log("程序执行结束!");
    
  3. 以上代码执行结果如下:
    $ node main.js
    菜鸟教程官网地址:www.runoob.com
    
    程序执行结束!
    

非阻塞代码

示例:

  1. 创建一个文件 input.txt ,内容如下:
    菜鸟教程官网地址:www.runoob.com
    
  2. 创建 main.js 文件, 代码如下:
    var fs = require("fs");
    
    fs.readFile('input.txt', function (err, data) {
        if (err) return console.error(err);
        console.log(data.toString());
    });
    
    console.log("程序执行结束!");
    
  3. 以上代码执行结果如下:
    $ node main.js
    程序执行结束!
    菜鸟教程官网地址:www.runoob.com
    


如上,阻塞是按顺序执行的,而非阻塞是不需要按顺序的,所以如果需要处理回调函数的参数,我们就需要写在回调函数内。