[
node-sync is a simple library that allows you to call any asynchronous function in synchronous way.
]
nodejs的最大特点,事件驱动,异步非阻塞,有且只有一个线程。
(1)异步的初步理解
nodejs包含大量异步过程和回调函数(callback),下面的代码实现了ls的功能。
# functions like ls command
# result <filenames of ./> <end> <filenames of ./>
fs = require 'fs'
filenames = fs.readdirSync '.'
for filename in filenames
console.log filename
fs.readdir '.', (_err, filenames)->
for filename in filenames
console.log filename
console.log 'end'
可以看到,上面代码先调用了同步的readdir函数读取当前路径下的filename。nodejs官方文档中,fs一节有很多file的同步实现函数及对应非同步函数,链接: http://nodejs.org/api/fs.html
版权声明:本文为u012492282原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。