On Monday, February 11, 2013 8:40:31 AM UTC+7, Suraj Singh Thapa wrote:
>
> Thanks Martin,
>
> Returning function resolved the issue,
>
> module.exports = function() {
> var spawn = require('child_process').spawn,
> ls = spawn('ls', ['-lh', '/usr']);
>
> ls.stdout.on('data', function (data) {
> return data;
> });
>
> ls.stderr.on('data', function (data) {
> return data;
> });
>
> ls.on('exit', function (code) {
> return code;
> });
> };
>
> As my requirement is,
>
> --> When a user click refresh button I want this function to execute and
> get the data and load it on index page.
> --> But I do not want, process to send the whole page back to server. So
> that user don't have to wait until above function returns the data back to
> client.
>
> Hope this make sense, Please do suggest if the approch I explained above
> is correct and I am in right track.
>
> Thanks.
>
>
You cannot return value from asynchronous handler because no one get it.
Your returns belong to inner callbacks not to outer function. I would write
like this:
module.exports = function(callback) {
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
callback(data);
});
ls.stderr.on('data', function (data) {
callback(data);
});
ls.on('exit', function (code) {
callback(code);
});
};
Or simpler:
module.exports = function(callback) {
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', callback);
ls.stderr.on('data', callback);
ls.on('exit', callback)
};
In *routes/index.js*
var getData = require('../data.js');
exports.index = function(req, res){
getData(function(data) {
res.render('index', { title: 'Profiles', data: data});
}
};
--
--
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
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.