0

This is a function which parses all the usb drives from /dev folder of a Raspberry Pi. I want to return sda, ada1, sdb, sdb1 as an array, but failed to do so. It does not print out anything when I do console.log(readDeviceList()). What's wrong with my code?

var usbDeviceList = new Array();

function readDeviceList() {
    var usbDeviceList = new Array();
    fs.readdir(deviceDir, function (error, file) {
        if (error) {
            console.log("Failed to read /dev Directory");
            return false;
        } else {
            var usbDevCounter = 0;
            console.log("Find below usb devices:");
            file.forEach(function (file, index) {
                if (file.indexOf(usbDevicePrefix) > -1) {
                    usbDeviceList[usbDevCounter++] = file;
                }
            });
            console.log(usbDeviceList); // This prints out the array
        };
    });
    console.log(usbDeviceList);         // This does not print out the array
    return usbDeviceList;               // Is this return value valid or not?
}
aaron
  • 39,695
  • 6
  • 46
  • 102
eric
  • 27
  • 7
  • Do you define usbDevicePrefix anywhere? – Nick Oct 14 '17 at 03:01
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – PMV Oct 14 '17 at 03:02
  • @snapjs yes, i defined it above. there is no error when i run that code – eric Oct 14 '17 at 03:15
  • Read the [duplicate](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) and then the [fs docs](https://nodejs.org/api/fs.html) –  Oct 14 '17 at 03:17
  • thansk @PMV I will have a look, I think the problem is caused by async function i used in code – eric Oct 14 '17 at 03:19
  • thanks @ProfessorAllman I will have a check those post – eric Oct 14 '17 at 03:20

1 Answers1

1

fs.readdir is an async function that takes a callback.

You can either propagate that callback:

function readDeviceList(callback) {
    var usbDeviceList = new Array();
    fs.readdir(deviceDir, function (error, file) {
        if (error) {
            callback(null, error);
        } else {
            // ...
            callback(usbDeviceList, null);
        };
    });
}

Or wrap it in a promise, which is easier to maintain:

function readDeviceList() {
    var usbDeviceList = new Array();
    return new Promise((resolve, reject) => {
        fs.readdir(deviceDir, function (error, file) {
            if (error) {
                reject(error);
            } else {
                // ...
                resolve(usbDeviceList);
            };
        });
    });
}

Usage:

// Callback
readDeviceList(function (usbDeviceList, error) {
    if (error) {
        // Handle error
    } else {
        // usbDeviceList is available here
    }
});

// Promise
readDeviceList.then(function (usbDeviceList) {
    // usbDeviceList is available here
}).catch(function (error) {
    // Handle error
});
aaron
  • 39,695
  • 6
  • 46
  • 102
  • hey @aaron, That's very useful. I'm quite new to javascript and I will have a look about your code, – eric Oct 14 '17 at 03:39