0

So I'm trying to run a few checks inside a file. Lets say inside checks.js I have

module.exports = async (content) => {
// Check no.1
if (content.id != 'SomeID') return;
// Check no.2
if (content.length > 20) return;
//..etc
}

And in my main file I am requiring this file. I want it to return in the original file depending on the outcome of checks.js So lets say the content id isn't the same as 'SomeID', I want it to return and not to continue the rest of the code in my main file. I did require('filePath')(content) But it doesn't actually return in the main file as it should by instructions in checks.js What am I doing wrong and what should I be doing. Thank you in advance!

QZAK
  • 63
  • 1
  • 6
  • 1
    its because you have defined it async, so at-least you need `await (require('filePath'))(content)`, though dont do it inline is ugly – Lawrence Cherone Nov 05 '20 at 00:43
  • This did not solve it. Can you also explain the logic behind your answer? Just so I'm more educated. – QZAK Nov 05 '20 at 00:49
  • what do you mean by *it doesn't actually return in the main file as it should*? whats `filePath`, maybe it should be `./checks` please show how you're using the file. as mentioned dont do `require('filePath')(content)` then you can debug it. – Lawrence Cherone Nov 05 '20 at 00:55
  • also your code as-is doesn't need to be async, have you checked what `content` actually is? plus... if content can be an object or an string/array you should check for that before the existing checks with typeof else if your passing in a string the first check would error – Lawrence Cherone Nov 05 '20 at 00:58
  • I know how to require a file lmao. It's a simple `require('./checks')(content)`. What I mean that it doesn't return is: If I put the id and length checks in the questions above in the main file it would return, meaning it would not proceed with the rest of the code but when I require the `checks` file instead of putting it directly then it does not return, it proceeds with the rest of the code in the main file which is not what I want. I want it to run the checks in `checks.js` and if a check tells it to return it won't run the rest of the main file code. Hope I'm making myself clear. – QZAK Nov 05 '20 at 01:00
  • The async part is not important at all, at the end of the day this is an example as I won't put the ENTIRE code and ALL the checks because I have tens of them (hence why I made this separate checks file). And checking for types is also not important, the main concept is explain above, everything else I can handle. – QZAK Nov 05 '20 at 01:02
  • abstracting out an if statement from your main code? then return true/false and wrap the main code in a big if statement `if (await (require('filePath'))(content)){...}` – Lawrence Cherone Nov 05 '20 at 01:02
  • The async part is important, if you're not awaiting its actually returning a Promise object and not what you think, do some basic debugging – Lawrence Cherone Nov 05 '20 at 01:03

1 Answers1

0

checks.js is returning an AsyncFunction, you must await it.

checks.js:

module.exports = async (content) => {
  // Check no.1
  if (content.id != 'SomeID') return;
  // Check no.2
  if (content.length > 20) return;
  //..etc

  return true // maybe your not returning truthy?
}

index.js:

const checks = require('./checks');

(async () => {

  console.log('typeof checks()', typeof checks);
  console.log('instance of', checks.constructor.name);

  //
  let content = {
    id: 'SomeID'
  };

  if (await checks(content)) {
    console.log('1. passed');
  } else {
    console.log('1. failed');
  }

  //
  content = {
    id: 'WrongID'
  };

  if (await checks(content)) {
    console.log('2. passed');
  } else {
    console.log('2. failed');
  }
})();

Will output when run:

typeof checks() function
instance of AsyncFunction
1. passed
2. failed
 

See Understanding async/await on NodeJS for more details.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106