Lanti
Lanti

Reputation: 2329

Returning the result of a Node.js asynchronous call outside of it

var glob = require('glob');
glob('*.jpg', { cwd: 'images/' }, function (err, files) {
  console.log(files);
});

--> I want to console.log(); here.

Forgive me that I don't know so basic stuff (probably the question also needs correction). How can I call the results of glob outside of it? If I want to console.log(); not inside glob (where console.log(files); resides)?

EDIT:

Following the linked resource, I successfully got the data out from the asynchronous call:

/*
http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call
https://github.com/maxogden/art-of-node#callbacks
*/
var globResults = undefined;
function globAsync(callback) {
  glob('*.jpg', { cwd: 'public/portfolio/weddings/', sort: true }, function (err, files) {
    var results = '\''+files.join('\',\'')+'\'';
    globResults = '[' + results + ']';
    callback();
  });
};

function globCaller() {
  console.log(globResults)
};

globAsync(globCaller);

Upvotes: 0

Views: 24

Answers (0)

Related Questions