Saturday, 8 April 2017

How to get data from function to variable

I'm learning node.js and I have a problem. How to get data from function to variable?

function loadOrInitializeTaskArray(file, cb) {
        fs.exists(file, function(exists) {
                var tasks = [];
                if (exists) {
                    fs.readFile(file, 'utf8', function(err, data) {
                            if (err) throw err;
                            var data = data.toString();
                            var tasks = JSON.parse(data || '[]');
                            cb(tasks);
                    })
                } else {
                        cb([]);
                }
        })
}



function listTasks(file) {
        loadOrInitializeTaskArray(file, function(tasks) {
                for (var i in tasks) {
                        console.log(tasks[i]);
                }
        })
}

Function listTasks works correctly, but I would like create own function, for example customListTasks:

function customListTasks(file, taskDescription) {
        var list = loadOrInitializeTaskArray(file, function(){});
        console.log(list);
}

This not return me errors, but console.log on list return me "undefined". How can I get this data to variable list?



via desger131

No comments:

Post a Comment