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.
It's asynchronous code. It cannot do return. It must have callback and respond after job done.
var figlet = require('figlet');
function art(dataToArt, callback)
{
figlet(dataToArt, function(err, data) {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return callback('');
}
callback(data);
});
}
art('Hello World', function (data){
console.log(data);
// also You can do operations here.
// for example can save to db or send to somewhere.
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With