For example we have a function to check the filesize:

const fs = require('fs');

function fileSize (fileName, cb) {
if (typeof fileName !== 'string') {
throw new TypeError('filename should be string')
} fs.stat(fileName, (err, stats) => {
if (err) {
return cb(err)
} cb(null, stats.size);
});
} fileSize(__filename, (err, size) => {
if (err) throw err; console.log(`Size in KB: ${size/}`);
});
console.log('Hello!'); /*
Hello!
Size in KB: 0.44921875
*/

It works fine, but the 'fileSize' function has a problem,

if (typeof fileName !== 'string') {
return new TypeError('filename should be string')
}

those part of code run in sync, not async, but the rest part of code for 'fileSize' is aysnc function. Normally a function should be always sync or async.

Why? If we call the fileSize with wrong params:

fileSize(, (err, size) => {
if (err) throw err; console.log(`Size in KB: ${size/}`);
});

It ouput:

/*
throw new TypeError('filename should be string')
^ TypeError: filename should be string
*/

Our console.log() is not running...

To fix it we can use 'process.nextTick', it run before 'event loop' and right after 'call stack is empty':

const fs = require('fs');

function fileSize (fileName, cb) {
if (typeof fileName !== 'string') {
return process.nextTick(
cb,
new TypeError('filename should be string')
)
} fs.stat(fileName, (err, stats) => {
if (err) {
return cb(err)
} cb(null, stats.size);
});
} fileSize(, (err, size) => {
if (err) throw err; console.log(`Size in KB: ${size/}`);
});
console.log('Hello!');
/*
Hello!
C:\Users\z000879\learn\maybe\src\process.js:21
if (err) throw err;
^ TypeError: filename should be string
*/

This time, our 'Hello' was printed out before error was throw.

[Node.js] process.nextTick for converting sync to async的更多相关文章

  1. Node.js & process.env & OS Platform checker

    Node.js & process.env & OS Platform checker Window 10 Windows 7 ia32 CentOS $ node # process ...

  2. Node.js process 模块常用属性和方法

    Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...

  3. Node.js 101(2): Promise and async

    --原文地址:http://blog.chrisyip.im/nodejs-101-package-promise-and-async 先回想一下 Sagase 的项目结构: lib/ cli.js ...

  4. how to config custom process.env in node.js

    how to config custom process.env in node.js process.env APP_ENV NODE_ENV https://nodejs.org/api/proc ...

  5. JavaScript简明教程之Node.js

    Node.js是目前非常火热的技术,但是它的诞生经历却很奇特. 众所周知,在Netscape设计出JavaScript后的短短几个月,JavaScript事实上已经是前端开发的唯一标准. 后来,微软通 ...

  6. node.js global object,util and so on

    核心模块主要内容: 全局对象 常用工具 事件机制 文件系统访问 http服务器和客户端 global object: 所有的全局变量(除了global本身外)都是global object 的属性(a ...

  7. 使用Node.js完成的第一个项目的实践总结

    http://blog.csdn.net/yanghua_kobe/article/details/17199417 项目简介 这是一个资产管理项目,主要的目的就是实现对资产的无纸化管理.通过为每个资 ...

  8. [转]Node.js tutorial in Visual Studio Code

    本文转自:https://code.visualstudio.com/docs/nodejs/nodejs-tutorial Node.js tutorial in Visual Studio Cod ...

  9. Practical Node.js摘录(2018版)第1,2章。

    大神的node书,免费 视频:https://node.university/courses/short-lectures/lectures/3949510 另一本书:全栈JavaScript,学习b ...

随机推荐

  1. Programming internal SRAM over SWD

    https://github.com/MarkDing/swd_programing_sram // // Copyright (c) 2013 SILICON LABORATORIES, INC. ...

  2. USB with NXP Microcontrollers

    USB with NXP Microcontrollers NXP Advantages NXP's microcontroller portfolio features the latest USB ...

  3. Revit API过滤管道系统类型

    管道只能通过PipeType过滤出来类型属性,只能是系统族的类型属性.管道实例过滤不能用族符号和族实例,要用Pipe using System; using System.Collections.Ge ...

  4. String Matching(poj1580)

    /*String Matching Description It's easy to tell if two words are identical - just check the letters. ...

  5. iOS开发UI篇章 15-项目中的常见文件

    iOS开发UI篇-常见的项目文件介绍 一.项目文件结构示意图 二.文件介绍 1.products目录:主要用于mac电脑开发的可运行文件.ios开发用不到这个文件 2.frameworks目录主要用来 ...

  6. SharePoint 应用程序页匿名

    前言 最近,有朋友问开发应用程序页,都是需要先登录再访问,无法开发匿名的应用程序页. 解决方法 其实,SharePoint帮我们提供了匿名访问的应用程序页的方法,只是和普通应用程序页继承的基类不一样, ...

  7. ios成长之每日一遍(day 4)

    今天, 主要讲四种常见的问题, 废话不多说了, 直接开始. 自动布局:这个我发现有一篇文章写得非常好, 直接表明出地http://www.cocoachina.com/applenews/devnew ...

  8. 录制Android屏幕软件——屏幕录像专家

    本文不是技术文章,今天分享下录制屏幕的软件.这个软件的效果还是不错的,前提是需要Root.软件名字:屏幕录像专家 来源网址:http://www.mumayi.com/android-350180.h ...

  9. [Hook] 跨进程 Binder设计与实现 - 设计篇

    cp from : http://blog.csdn.net/universus/article/details/6211589 关键词 Binder Android IPC Linux 内核 驱动 ...

  10. 使用ASP.NET读取word2003文档

    直接使用.NET 读取doc文档. http://www.codeproject.com/Articles/22738/Read-Document-Text-Directly-from-Microso ...