Asynchronous fs.stat.isDirectory()
2016-02-26 13:21
169 查看
function showFile() { for(var i = 0; i< files.length; i++){ var itemFile = files[i]; fs.stat(__dirname + itemFile, function (err, stat){ // if (stat.isDirectory()) { console.log(' '+i+' \033[36m' +itemFile + '/\033[39m'); // }else{ console.log(' '+i+' \033[90m' + itemFile + '\033[39m'); } }); } process.stdout.write('\033[33mEnter your choice: \033[39m'); }
上面的代码段在语法上市没有问题的,在执行的时候抛出了错误:
TypeError: Cannot read property 'isDirectory' of undefined at ...\index.js:46:13 at FSReqWrap.oncomplete (fs.js:82:15)
这里错误在于使用synchronous的for循环.
如果使用下面的方法代替showFile(),it works!
function file(i) { var filename = files[i]; /** * fs.stat : */ fs.stat(__dirname + '/' + filename, function (err, stat){ // if (stat.isDirectory()) { console.log(' '+i+' \033[36m' +filename + '/\033[39m'); // }else{ console.log(' '+i+' \033[90m' + filename + '\033[39m'); } i++; if (i== files.length) { console.log(''); process.stdout.write('\033[33mEnter your choice: \033[39m'); process.stdin.resume(); }else{ file(i); } }); }
相关文章推荐
- 启动mysql出现1067错误
- Mybatis generator自动生成代码的几种方法
- C++阻塞队列实现
- Android View的事件分发机制(一):View
- golang实现常用数据结构
- JAVA String.format 方法使用介绍
- MySQL字符集编码
- Spark SQL: Error in query: undefined function错误的解决方法
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)(转载)
- 8.UIAlertView
- Android LayoutInflater(布局填充器)的详解
- ArrayList的一点研究
- 2015年中国大数据公司年度排行榜
- Winscock 的I/O模式,阻塞、非阻塞、重叠、同步与异步
- ASP.NET内置对象的总结
- bzoj 1013: [JSOI2008]球形空间产生器sphere 高斯消元
- ioS开发知识(二十七)
- js实现复制(zclip)
- C语言内存四区
- Java学习笔记(三):数组