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?
}
6
  • Do you define usbDevicePrefix anywhere? Commented Oct 14, 2017 at 3:01
  • 1
    Possible duplicate of How do I return the response from an asynchronous call? Commented Oct 14, 2017 at 3:02
  • @snapjs yes, i defined it above. there is no error when i run that code Commented Oct 14, 2017 at 3:15
  • Read the duplicate and then the fs docs Commented Oct 14, 2017 at 3:17
  • thansk @PMV I will have a look, I think the problem is caused by async function i used in code Commented Oct 14, 2017 at 3:19

1 Answer 1

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
});
Sign up to request clarification or add additional context in comments.

1 Comment

hey @aaron, That's very useful. I'm quite new to javascript and I will have a look about your code,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.