node.js callback

fs = require('fs')
fs.readdir ".", (err, filenames) ->
    for fi in filenames
        console.log fi
    console.log "ready"        

console.log 'end'

dengwei@V1088:~/node_test$ ls
a.coffee app.js circle.js d.coffee echo.js example.js

output:

end

circle.js

example.jsecho.js

d.coffeeapp.js

a.coffee

ready

 see another way of callback:

fs = require 'fs'
print = (input) ->
    console.log input

getDir = (path, cal) ->
    fs.readdir path, (err,files) ->
        if err
            console.log err
        else
            filesName = files[1]
        cal err, filesName
        console.log 'finished'


console.log '1'

fn = getDir '.',(err, input) ->
    console.log input
    console.log 'finished fn'

console.log 'end'

output:

1
end
example.js
finished fn
finished

  1

fs = require 'fs'
print = (err, input) ->	
    #console.log arguments
    console.log input

getDir = (path, cal) ->
    fs.readdir path, (err,files) ->
        if err
            console.log err
        else
            filesName = files[1]
        cal err, filesName


console.log '1'

#fn = getDir '.',(err, input) ->
#    console.log input
#    console.log 'finished fn'

getDir '.', print

console.log 'end'

  2

fs = require 'fs'
print = (input) ->
    console.log input

getDir = (path, cal) ->
    fs.readdir path, (err,files) ->
        if err
            console.log err
        else
            filesName = files[1]
        cal filesName


console.log '1'

getDir '.', print

console.log 'end'

3

fs = require 'fs'

getDir = (path, cal) ->
    fs.readdir path, (err,files) ->
        if err
            console.log err
        else
            filesName = files[1]
        cal err, filesName


console.log '1'

fn = getDir '.',(err, input) ->
    console.log input
    console.log 'finished fn'


console.log 'end'

  EOF

 

 

posted @ 2013-03-26 22:05  邓维  阅读(753)  评论(0编辑  收藏  举报