0

I am trying to load data which is in javascript files into an object.

All the files are like this:

module.exports = {
  test: ‘qwerty’
}

I’m using require, but I have to load several directories so need to do the loads one at a time.

I tried wrapping it in a promise:

function load(fileOrDirPath) {
  return new Promise(function(resolve, reject) {
    let data;

    debug(`Requiring: [${fileOrDirPath}]`);

    try {
      data = require(fileOrDirPath);
    }
    catch(e) {
      return reject(e);
    }

    debug(`Loaded data: [${JSON.stringify(data, 0, 2)}]`);

    return resolve(data);
  });
}

I use the function:

const dirPath = ‘redacted’
const data = await load(dirPath);
debug(`Loaded: [${JSON.stringify(data, 0, 2)}]`);

And the logs show that the data is loaded inside the function. However outside the data is always null.

Why doesn’t the function await?

I tried looking on npm for a module but couldn’t find any.

How can I load a directory of javascript files recursively into an object?

vy218
  • 707
  • 1
  • 8
  • 15
  • 1
    Can you elaborate on your use case? Why do you want to wrap `require` in `async/await` if you need to load the files one after another? Or is it that you just don't want to do it synchronously? If the latter is the case you cannot use `require` as it's synchronous anyway. – eol Jan 24 '22 at 12:42
  • But I’ve wrapped it in a promise, since it takes a while to load, why doesn’t that work? – vy218 Jan 24 '22 at 12:47
  • 1
    I encourage you to read this https://stackoverflow.com/a/20528452/3761628 - it explains it pretty well. – eol Jan 24 '22 at 12:52
  • Thanks @eol - The link you sent is about callbacks, I’m trying to do it using promises, is that not possible? – vy218 Jan 24 '22 at 12:58
  • I don’t understand why the logs show the loading is happening in the right order, but it’s still null outside the function. It appears to be both loaded and not loaded at the same time. How is that even possible? – vy218 Jan 24 '22 at 12:59
  • From the statement `data = require(fileOrDirPath);`, you should already understand that `require()` is synchronous, and that "doing the loads one at a time" is the natural behaviour. Unless you are after some fancy visual effect such as a progress thermometer, then there's no point in trying to promisify a `require` sequence. – Roamer-1888 Jan 24 '22 at 18:51
  • I don’t mind doing the loads one at a time (though any optimisation would be great). The issue is that the data never gets loaded in time, and so all the code that is waiting to use the loaded data fails because it is null. How do I even get it to load one at a time properly? – vy218 Jan 24 '22 at 23:36
  • By the way…it’s currently running in a for loop with an await, so 1 at a time is what I’m attempting to do, but even that doesn’t work. – vy218 Jan 24 '22 at 23:44
  • Promises (as used) can only make the data available later, not earlier. – Roamer-1888 Jan 25 '22 at 00:19
  • @roamer thanks - Could you elaborate on how I would go about using promises in a way that would enable my later code to use the loaded data? – vy218 Jan 25 '22 at 07:44
  • I can't advise as I don't fully understand the problem. Could be a dodgy js implementation or polyfill for `require()`? – Roamer-1888 Jan 25 '22 at 14:50
  • Thanks for the comments - I figured out a way forward that works. I was all set in my mind that I wanted a utility in my app to load the data. After the discussion on this thread, I realised that I might as well just structure my app/library so the end user requires their own data, then passes it into the main code. So far it appears to be working :) – vy218 Jan 26 '22 at 23:36

0 Answers0