# Weird asynchronous behaviour in p5.js

**URL:** <https://discourse.processing.org/t/weird-asynchronous-behaviour-in-p5-js/38072>\
**Category:** p5.js\
**Created:** [July 24, 2022, 12:39pm UTC](https://discourse.processing.org/t/weird-asynchronous-behaviour-in-p5-js/38072 "2022-07-24T12:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![M1ke](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@M1ke](https://discourse.processing.org/u/M1ke)\
**Post date:** [July 24, 2022, 12:39pm UTC](https://discourse.processing.org/t/weird-asynchronous-behaviour-in-p5-js/38072/1 "2022-07-24T12:39:05Z")

</div>

In short: I’m making a programming language in p5.js as an exercise, so nothing too serious, however I have incountered a problem which seems to be unique to p5.js . For example, my compiler interprets these 3 successive lines of code “print hey” , “alert”, “print done” and should call IN ORDER the functions “console.log(“hey”)” , “alert()” and "console.log(“done”). However instead I get the alert popup first, then all the console logs together at the end, almost as if p5 was treating the code asynchronously and choosing to perform certain operations before others instead of following the order in which the compiler “gets” them. I tried rewriting the compiler with base javascript and the issue doesn’t exist there, with the same exact code. So why is this? I also tried to fiddle with “async” and “await” but to no avail.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [July 24, 2022, 1:30pm UTC](https://discourse.processing.org/t/weird-asynchronous-behaviour-in-p5-js/38072/2 "2022-07-24T13:30:32Z")

</div>

Hi @M1ke,

Depends ond the implementation of console.log which could be implemented to be buffered writer.

Cheers  
— mnse

PS: Don’t call it compiler, but rather interpreter. [What’s the difference?](https://www.geeksforgeeks.org/difference-between-compiler-and-interpreter/)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 24, 2022, 2:04pm UTC](https://discourse.processing.org/t/weird-asynchronous-behaviour-in-p5-js/38072/3 "2022-07-24T14:04:57Z")

</div>

Hello @M1ke,

This topic may be of interest:

> [@Printing a Vector before and after changing it produces unexpected results](https://discourse.processing.org/t/printing-a-vector-before-and-after-changing-it-produces-unexpected-results/32111/1):
>
> I’m putting together a short exercise for my students on p5.Vector and its operations. The output of the code below surprises me, and I was hoping someone could explain what’s going on. I’ve been programming in Processing for years, but am new to p5.js, and so my unexpected results might have more to do with how javascript works (or how objects work, or how console output works). Here is the code, along with comments indicating the expected results: function setup() { let h = new p5.Vector(3…

`:)`

---

<div class="post-metadata">

**Author:** ![M1ke](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@M1ke](https://discourse.processing.org/u/M1ke)\
**Post date:** [July 24, 2022, 2:06pm UTC](https://discourse.processing.org/t/weird-asynchronous-behaviour-in-p5-js/38072/4 "2022-07-24T14:06:44Z")

</div>

I see, so I guess there is no solution but to use javascript… also for the name: you’re absolutely right, but at the very beginning of this project I had made a custom RAM symulation as well, that stored the program in binary, created from a personalized assembly to which the code was compiled to. Now, hovewer, it’s just an interpreter from my language to js, as you said. Thank you for the answer anyway!
