Loop equivalent in JS

Is it possible to make a loop that behaves like the loop in the events. Problem is i want to use show choices inside it and i cant make it work inside a plugin. Also i want to use sounds and show pictures inside the loop. Im sure thats not an easy task, but any help would be aprreciated. I also looked through the internet, read the api documentation, but nothing comes close to what i want to do.
There are a number of kinds of loops in JavaScript - they're pretty basic commands that should come up right away when you Google it.

Is there a good reason you're outsourcing this to a plugin? It really sounds like the kind of thing that would be easier to create in an event, especially if you're not very knowledgeable with JavaScript.
I agree with Turan. You should strongly consider using eventing for this.
Yeah i know about basic loops like while and for loops, but they dont behave like the loop in the Events. For example showchoices and the wait commands dont work. I also tried to play sound effects, but when you call two of them, only one gets played. At least i didnt get them to work.

And i am outsourcing it firstly because i want to learn some js, but also because the code is so much more maintainable in js, than in an event. And i have so much more options for example i need a lot of switch cases which are ugly to do in events with a thousand if statements.
Script commands are self-contained, so you can't have event commands in between them; therefore a Javascript loop wouldn't do what you want it to. If you wanted to have choices and wait commands in the loop, you'd have to code them, and coding a choice list, while doable, is fairly complex.
Yeah i know about basic loops like while and for loops, but they dont behave like the loop in the Events.
Well, they do. The Loop event command is functionally the same as you saying
Code:
while (true)
It will loop until you break it, which is exactly what the event loop does.

For example showchoices and the wait commands dont work. I also tried to play sound effects, but when you call two of them, only one gets played.
This isn't a matter of loops behaving differently, this is because you're trying to use individual JavaScript commands outside of the construct of the game interpreters. That's what the event system is for.

Using code in RPG Maker is a balance - you have to know what is a worthwhile, efficient thing to outsource to JavaScript and what is easier to keep in events, as @Trihan describes. It doesn't make sense to try to do everything in code just because you can.
What would happen if i made the entire thing async? I mean the main gameloop would continue and could register calls to play audio or stuff. I am not sure if thats exactly how it works and i think it could also cause lots of bugs

Original thread: https://forums.rpgmakerweb.com/threads/loop-equivalent-in-js.159460/