zoukankan      html  css  js  c++  java
  • Node.js回调函数

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

    异步编程依托于回调来实现,但是不能说使用了回调后程序就异步话了,回调函数在完成任务之后会被调用,Node使用了大量的回调函数,Node所以的API都是支持回调函数的。如下是阻塞和非阻塞的例子。

    阻塞代码实例:

    首先创建一个文件input.txt,如下内容:

    这个是一个测试文件,O(∩_∩)O哈哈哈~。

    创建main.js,代码如下:

    var fs=require("fs");

    var data=fs.readFileSync(‘input.txt’);

    console.log(data.toString());

    console.log("the program  execute to the end");

    非阻塞代码:

    main.js的代码:

    fs=require('input.txt');

    fs.readFile('input.txt',function(error,data){

    if(error)return console.error(error);

    console.log(data.toString());

    });

    console.log("the program  execute to the end");

  • 相关阅读:
    订单号设计
    小公司的技术架构原则
    Redis配置详解
    实现图片懒加载
    Js的GC机制
    防抖与节流
    Js中的堆栈
    浏览器窗口间通信
    块级格式化上下文
    实现瀑布流布局
  • 原文地址:https://www.cnblogs.com/VARForrest/p/7930406.html
Copyright © 2011-2022 走看看