Explore Library
Code Quiz

Non-blocking I/O Return Bug

Spot why this function returns undefined despite reading a file correctly.

Codejavascript
const fs = require('fs');

function readConfig() {
  let config;
  fs.readFile('./config.json', 'utf8', (err, data) => {
    if (err) throw err;
    config = JSON.parse(data);
  });
  return config;
}

const c = readConfig();
console.log(c.port); // TypeError: Cannot read properties of undefined

What is the bug in this code, and how should it be fixed?