[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 ...
随机推荐
- UVA 11732 - strcmp() Anyone?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- Delphi取UTC时间秒
自格林威治标准时间1970年1月1日00:00:00 至现在经过多少秒数时间模块Uses DateUtils;当前时间:中国是 +8时区,换成UTC 就要减掉8小时showMessage(intt ...
- sql select 0 字段
关于 select 语句中 0 某字段名字,的意思是:该某字段是不在指定的表中的,那么如果要在子查询中利用这个指定的表,且带有这个字段,那么就用这个方式来添加该字段,并以0来初始化该字段. 如下例: ...
- Spring Boot开发之明月千城(一)
原文地址:http://qindongliang.iteye.com/blog/2205633 最近数据分析的项目也即将告一段落了,中间也积累了很多知识,特此记录一下.其中用的最爽的Web组合开发就是 ...
- require.js 简洁入门
原文地址:http://blog.sae.sina.com.cn/archives/4382 前言 提到require.js大多数人会说提到模块化开发,AMD等等,其实require.js并没有这么多 ...
- 根据Request ID找到对应的Session信息
2018年3月15日 13:04 /* Formatted on 2018/3/15 13:04:45 (QP5 v5.256.13226.35538) */ --根据Request ID找到对应的S ...
- 1300多万条数据30G论坛大数据优化实战经验小结
最近由于某大型网站社区论坛运行效率比较低用户反馈论坛有些卡需要对系统进行优化,论坛性能影响了公司的形象还有网站的流量,当然这也会影响到公司的收入,而且后期还需要长期维护网站的社区论坛服务. 1:并发访 ...
- Hadoop-2.2.0中文文档—— Common - Native Libraries Guide
概览 这个新手教程描写叙述了native(本地?原生?)hadoop库,包括了一小部分关于native hadoop共享库的讨论. This guide describes the native ha ...
- Java判断多个时间段是否重叠(重叠区间个数)
import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * 判断多个时间段是否出现重叠 ...
- 进一步优化ListView
之前我已经分享过一篇:viewHodler的通用写法,就是专门用来优化listview的加载的,但是对于复杂的布局,我们还需要在listview滑动和不滑动时进行自己的处理,今天我看到一篇文章就是讲这 ...