On Tue, Sep 11, 2012 at 8:29 AM, António Ramos <[email protected]> wrote:

>  What i want is to create a function to read files and return the files
> to the caller.
>
> This code is not working!
>
> my func in coffeescript
>
> fs = require('fs')
>
> InitLoadTasks =->
>   a=[]
>   fs.readdir __dirname+ '/toRun', (err, files) ->
>     if (err)
>       console.log(err)
>     a=files
> initLoadTasks()
>
> reads files from directory
>
> how do i return the a array as a return value to InitLoadTasks?
>

The initLoadTasks function does something asynchronous, so you need to pass
it a callback function that will be invoked when it finishes.
If the only thing you want is a list of the files in a given directory, you
don't need to write initLoadTasks. You can just call fs.readdir.
However, supposing you want to do something more, here's how you could
write it in JavaScript.

var fs = require('fs');

function initLoadTasks(subDir, cb) {
  fs.readDir(__dirname + '/' + subDir, function (err, files) {
    if (err) {
      return cb(err);
    }
    // do extra processing here
    cb(null, files);
  });
}

initLoadTasks('toRun', function (err, files) {
  // do something with files here after checking err
});

-- 
R. Mark Volkmann
Object Computing, Inc.

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to