Node.js学习 - CallBack Function
2016-04-15 22:16
459 查看
Node.js异步编程的直接体现就是回调,Node使用了大量的回调函数,其所有的API都支持回调。
阻塞代码实例(同步)
非阻塞代码实现(异步)
阻塞代码实例(同步)
var fs = require("fs"); var data = fs.readFileSync('input.txt'); #读取文件,程序阻塞在这里,等待读取完毕再往下进行 console.log(data.toString()); console.log('Program stop!');
非阻塞代码实现(异步)
var fs = require('fs'); fs.readFile('input.txt', function (err, data) { #读取文件,程序不暂停,继续往下执行 if (err) return console.error(err); console.log(data.toString()); }); console.log('Program stop!')
相关文章推荐
- Node 学习笔记之事件发射器
- Node.js学习 - Install and Configure
- 116. Populating Next Right Pointers in Each Node
- Nodejs建站笔记-注册登录流程的简单实现
- 237.LeetCode Delete Node in a Linked List(easy)[单链表 删除节点]
- 使用express(4.X)搭建第一个Web应用【Node.js初学】
- Installing node.js on ubuntu server 15.10
- 『奇葩问题集锦』npm install 报错 node-pre-gyp ERR! node-pre-gyp -v v0.6.25
- node authentication
- Unix Filesystem Organization
- 美团酒店Node全栈开发实践
- 理解Nodejs中的事件轮询机制
- webstorm中使用nodejs,npm 配置
- NodeJS、NPM安装配置步骤
- 错误:namenode无法自动切换成active
- Nodejs Express目录结构
- 19. Remove Nth Node From End of List
- NodeJS、NPM安装配置步骤(windows版本)
- nodejs利用nodemailer发送邮件
- node解析ini文件