微信小程序 Node.js (基础三) 回调函数

来源:互联网 发布:软件定做平台 编辑:程序博客网 时间:2024/05/29 04:08

Node.js 回调函数 阻塞与非阻塞
Node.js 异步编程的直接体现就是回调。
异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。
回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都支持回调函数。
阻塞代码实例(同步函数)

//阻塞是按顺序执行的var fs = require("fs");var data = fs.readFileSync('input.txt');console.log(data.toString());console.log("程序执行结束!");

非阻塞实例(异步函数)

//非阻塞是不需要按顺序的var fs  = require("fs")fs.readFileSync('ipnut.txt',function(err,data){    if(err) return console.log(err)    console.log(data.toString())})console.log("程序执行结束!");
原创粉丝点击