1.Node异步编程

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

异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。

回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都支持回调函数。

例如,我们可以一边读取文件,一边执行其他命令,在文件读取完成后,我们将文件内容作为回调函数的参数返回。这样在执行代码时就没有阻塞或等待文件 I/O 操作。这就大大提高了 Node.js 的性能,可以处理大量的并发请求。

//server.js
var http = require("http");
var openfile = require("./openfile"); http.createServer(function(res, res){
res.writeHead(200, {"Content-Type":"text/html; charset=uf-8"});
if (res.url!=="/favicon.ico") {
console.log('begin visit');
res.write('hello world');
openfile.readfile('./test.txt');
console.log('异步加载完毕');
res.end('end');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
// readfile.js
var fs = require("fs"); module.exports = { //异步读取
readfile: function(path){
fs.readFile(path, function(err, data){
if (err) {
console.log(err)
} else {
console.log(data.toString());//data为一个buffer
}
})
}, //同步读取
readfileSync:function(path){
var data = fs.readFileSync(path, 'utf-8');
console.log(data.toString());//data为一个buffer
console.log("同步方法执行完毕");
return data;
}
}
// test.txt
test-messagewelcome!

异步:

同步:

node基础06:回调函数的更多相关文章

  1. Node.js系列基础学习-----回调函数,异步

    Node.js基础学习 Node.js回调函数 Node.js异步编程的直接体现就是回调,异步编程依托回调来实现,但不是异步.回调函数在完成任务后就会被调用,Node有很多的回调函数,其所有的API都 ...

  2. Node.js:回调函数

    概要:本篇博客主要通过对比node.js异步与同步方式读取文件的方式来解释node中回调函数的作用. 1.异步方式读取文件: 新建一个文本文档用于测试,如下图: 代码如下: // node异步方式读取 ...

  3. 【node.js】回调函数

    学习链接:http://www.runoob.com/nodejs/nodejs-callback.html Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调 ...

  4. Node js 安装+回调函数+事件

    /* 从网站 https://nodejs.org/zh-cn/ 下载 这里用的 9.4.0 版本 下载完安装 安装目录是 D:\ApacheServer\node 一路默认安装 安装后打开cmd命令 ...

  5. nodejs基础(回调函数、模块、事件、文件读写、目录的创建与删除)

    node官网:http://nodejs.cn/ 今天想看看node的视频,对node进一步了解, 1.我们可以从官网下载node到自己的电脑上,今天了解到node的真正概念,node时javascr ...

  6. Node.js 阻塞 回调函数

    回调例程 N所有API都支持回调函数,可以处理大量并发请求.回调函数一般作为最后一个参数出现: function foo1(name, age, callback){ } function foo2( ...

  7. node.js 中回调函数callback(转载),说的很清楚,看一遍就理解了

    最近在看 express,满眼看去,到处是以函数作为参数的回调函数的使用.如果这个概念理解不了,nodejs.express 的代码就会看得一塌糊涂.比如: 复制代码 代码如下: app.use(fu ...

  8. go语言基础之回调函数

    1.回调函数 示例: 实现:加,减,乘 示例: package main import "fmt" type FuncType func(int, int) int //实现加法 ...

  9. javascript基础之回调函数

    简单来说,回调函数:也就是将要执行的函数. 回调函数具体的定义为:函数A作为参数(函数引用)传递到另一个函数B中,并且这个函数B执行函数A.我们就说函数A叫做回调函数.如果没有名称(函数表达式),就叫 ...

随机推荐

  1. php 图片上传 使用微秒做文件名

    $m = microtime ();$mtime = explode(' ' ,$m);$mtime1 = $mtime[1];$mtime2 = substr($mtime[0], 2, 6);$p ...

  2. display:inline-block 去除间隙

    先写出代码 核心css代码: .sample0{display: inline-block;height: 40px;width: 40px;color: #ffffff;background-col ...

  3. css3:盒模型以及box-sizing属性

    文档中的每个元素被描绘为矩形盒子.渲染引擎的目的就是判定大小,属性——比如它的颜色.背景.边框方面——及这些盒子的位置.在CSS中,这些矩形盒子用标准盒模型来描述.这个模型描述了一个元素所占用的空间. ...

  4. 联通烽火hg220桥接tplink路由器

    转载请注明:from http://www.cnblogs.com/juandx/p/5544517.html 1.普通账户:user(7fnmmnml,默认是bdettbr0),猫背后可见 2.工程 ...

  5. C#中去掉表中重复的数据

    /// <summary> /// 去掉表中重复的数据  int /// </summary> /// <param name="SourceTable&quo ...

  6. Linux Buffer I/O error on device dm-4, logical block

    Linux服务器日志(Oracle Linux Server release 5.7)里面出现了一些"Buffer I/O error on device dm-4, logical blo ...

  7. Java集合分组

    public class Data { private Long id ; private Long courseId ; private String content ; public Long g ...

  8. HTTPS那些事(二)SSL证书

    转自:http://www.guokr.com/post/116169/ 从第一部分HTTPS原理中, 我们可以了解到HTTPS核心的一个部分是数据传输之前的握手,握手过程中确定了数据加密的密码.在握 ...

  9. openstack-kilo--issue(十一)Failed connect to 169.254.169.254:80; No route to host

    # curl http://169.254.169.254/latest/user-data curl: () Failed connect to ; No route to host 解决方案: c ...

  10. RedHat 6.2 Linux修改yum源免费使用CentOS源

    在没有光盘的情况,需要安装软件包,就要用到共网的yum源来安装了. RedHat linux 默认是安装了yum软件的,但是由于激活认证的原因让redhat无法直接进行yum安装一些软件,如果我们需要 ...