function art(dataToArt){
var figlet = require('figlet');
var result;
figlet(dataToArt, function(err, data) {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return;
}
var result = data;
});
return result;
}
test = art('Hello World');
console.log(test);
Running this gives "undefined". How to access the changes made by function figlet to the variable result defined outside the function figlet.
figletis an asynchronous function. You can't. You need to do all your work inside the callback.resultin multiple scopes, so you're returning a differentresultthan you're assigningdatato. However, changing that (result = data;withoutvar) won't fix the issue of execution timing.dataof your asynchronous callback function.