[Node.js] process.nextTick for converting sync to async
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的更多相关文章
- Node.js & process.env & OS Platform checker
Node.js & process.env & OS Platform checker Window 10 Windows 7 ia32 CentOS $ node # process ...
- Node.js process 模块常用属性和方法
Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...
- Node.js 101(2): Promise and async
--原文地址:http://blog.chrisyip.im/nodejs-101-package-promise-and-async 先回想一下 Sagase 的项目结构: lib/ cli.js ...
- 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 ...
- JavaScript简明教程之Node.js
Node.js是目前非常火热的技术,但是它的诞生经历却很奇特. 众所周知,在Netscape设计出JavaScript后的短短几个月,JavaScript事实上已经是前端开发的唯一标准. 后来,微软通 ...
- node.js global object,util and so on
核心模块主要内容: 全局对象 常用工具 事件机制 文件系统访问 http服务器和客户端 global object: 所有的全局变量(除了global本身外)都是global object 的属性(a ...
- 使用Node.js完成的第一个项目的实践总结
http://blog.csdn.net/yanghua_kobe/article/details/17199417 项目简介 这是一个资产管理项目,主要的目的就是实现对资产的无纸化管理.通过为每个资 ...
- [转]Node.js tutorial in Visual Studio Code
本文转自:https://code.visualstudio.com/docs/nodejs/nodejs-tutorial Node.js tutorial in Visual Studio Cod ...
- Practical Node.js摘录(2018版)第1,2章。
大神的node书,免费 视频:https://node.university/courses/short-lectures/lectures/3949510 另一本书:全栈JavaScript,学习b ...
随机推荐
- JTAG - General description of the TAP Controller states
A transition between the states only occurs on the rising edge of TCK, and each state has a differen ...
- 百度外卖接口调试 C#版
主类 class Program { static void Main(string[] args) { string cmdStr = &qu ...
- 如何:声明、实例化和使用委托(C# 编程指南)
委托的声明如下所示: C# public delegate void Del<T>(T item); public void Notify(int i) { } C# Del< ...
- 没有可用的复制构造函数或复制构造函数声明为“explicit”
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810) : error C2558: str ...
- DevExpress VCL for Delphi 各版本收集下载
更多VCL组件请到:http://maxwoods.400gb.com/u/758954/1974711 DevExpress VCL 5.7:http://www.ctdisk.com/file/7 ...
- ssh 多条命令执行
格式:ssh user@ip command 单条命令:ssh user@ip command1 多条命令:ssh user@ip "command1;command2" 不加双引 ...
- MEF and AppDomain z
MEF and AppDomain - Remove Assemblies On The Fly This article will give an idea of what's involved i ...
- nginx网站攻击防护
1.上上个月架构全部迁移上云以后,总的来说比较稳定,业务量也上来,可爱的坏人也来了,7X24小时不停恶意攻击我的网站,第一次收到报警是网站流入流量1分钟以内连续3次超过1000000bps,换算下1M ...
- mysql递归查询子类ID查询所有子类
先来看数据表的结构如下: id name parent_id --------------------------- 1 Home 0 2 About ...
- spring 5.0.1.RELEASE官方任然不支持velocity(平台升级)
官方说明: Dear Spring community, It is my pleasure to announce that Spring Framework 5.0.1 is available ...