0

I'm trying to loop through an enmap, but it doesn't seem to be working. I got forEach() to work, however, I need to be able to break out of it, which doesn't work with forEach(). Here's what I currently have

for(var id in bot.myenmap.fetchEverything()) {
  console.log("test")
}

I have no other details as I can provide, as it doesn't error or log anything. If you know how to loop over it, any help would be appreciated, thanks.

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Steven Webster
  • 308
  • 3
  • 11

1 Answers1

0

If you got forEach() to work, you may want to check this answer: it explains how to "break" out of a forEach() loop.
You have to put the loop inside a try block, when you want to break the loop you can throw a custom exception: in the catch statement you'll make it ignore your custom exceptions, while throwing the ones that you didn't plan (like it normally would). Here's an example:

// This is to declare the custom exception:
var BreakException = {};

try {
  bot.myenmap.forEach((element) => {
    // This is like a normal forEach loop
    // When you want to break it, just do this:
    throw BreakException;
  });
} catch (e) {
  // If it's a BreakException it will ignore it and not givce errors
  if (e !== BreakException) throw e;
}

Code adapted from the answer mentioned above.

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50