$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Async readFile module.exports in node.js - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Async readFile module.exports in node.js - Stack Overflow

matteradmin12PV0评论

I'm sorry for, what might easily be a naive question, but I`m trying to figure out how node works, especially for a problem like this:

What I need do is to send an object/file from fs.readFile through require and module.exports. This is what I have tried is this

in one file (call it app.js) the code for reading a file:

var fs = require('fs');
var file_contents = undefined;

var callback_reader = function(err, data) {
  if (err) return console.error(err);
  file_contents = data.toString().split('\n');
}

module.exports = {
  parseFile: function(file_path) {
    fs.readFile(file_path.toString(), 'utf-8', callback_reader);
  }
} 

and in some other file, (call it main.js) I need to use the contents of the file read by the readFile like this

var file_importer = require('./app.js')
file_importer.parseFile(real_path_to_file);

but if i try console.log of this last line I always get undefined object. Now I know it is because callback does not execute before the console.log but I`m unsure how to achieve this munication.

I'm sorry for, what might easily be a naive question, but I`m trying to figure out how node works, especially for a problem like this:

What I need do is to send an object/file from fs.readFile through require and module.exports. This is what I have tried is this

in one file (call it app.js) the code for reading a file:

var fs = require('fs');
var file_contents = undefined;

var callback_reader = function(err, data) {
  if (err) return console.error(err);
  file_contents = data.toString().split('\n');
}

module.exports = {
  parseFile: function(file_path) {
    fs.readFile(file_path.toString(), 'utf-8', callback_reader);
  }
} 

and in some other file, (call it main.js) I need to use the contents of the file read by the readFile like this

var file_importer = require('./app.js')
file_importer.parseFile(real_path_to_file);

but if i try console.log of this last line I always get undefined object. Now I know it is because callback does not execute before the console.log but I`m unsure how to achieve this munication.

Share Improve this question asked Jun 4, 2015 at 16:20 Fedja BlagojevicFedja Blagojevic 8471 gold badge10 silver badges18 bronze badges 2
  • You are not returning the file_contents from callback_reader and you need var file = file_importer.parse.... console.log(file) – Molda Commented Jun 4, 2015 at 17:09
  • Ok if I use return file_contents in app.js's callback_reader and call console log in main.js like @molda described I still get undefined in the log. – Fedja Blagojevic Commented Jun 4, 2015 at 18:02
Add a ment  | 

2 Answers 2

Reset to default 5

So i changed your code a little bit to use callbacks. It seems that you can't use "return" from asyncronous function in module.exports. However, the code bellow works as expected. Hope it helps.

main.js

var file_importer = require('./app.js')
file_importer.parseFile('./time.js', function(err, data){
    if(err) return console.log(err);
    console.log(data);
});

app.js

var fs = require('fs');

module.exports = {
    parseFile: function(file_path, callback) {
        fs.readFile(file_path.toString(), 'utf-8', function(err, data) {
            if (err) return callback(err);
            callback(null, data);
        });
    }
}

// much shorter version
exports.parseFile = function(file_path, callback) {
    fs.readFile(file_path.toString(), 'utf-8', callback);
}

This is javascript work, it don't wait the callback was called to return. You should do your console.log in your callback. Like these :

fs.readFile(pathToFile, 'utf-8', function(err, data) {
  if (err) return err;
  console.log(data);
  // Continue your process here
})
Post a comment

comment list (0)

  1. No comments so far