Problem with asynchronous code in class ES6 #1292
Replies: 23 comments
-
Posted at 2019-03-01 by asez73 Hi, Reading your question more carefully, your problem is the value of this in the setInterval, probably the global context which is not what you want.
So, your object, instance of class, will be called self in the setInterval/SetTimeout at execution time... You should look at the reference of setInterval and this non Espruino thread or this other one |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-01 by Konkery I disagree with you
It seems to me that the problem is in Espruino |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-01 by Robin Fri 2019.03.01 What version of Espruino is running/flashed? Not all ES6 supported as of this post |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-01 by asez73 However, Espruino is designed to run on very low level mcu's and it doesn't claim any real compatibility with nodejs or other standards. Maybe the Tessel board would suit you better? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-02 by @allObjects EDIT: ...to cut to the chase: Conclusion: Everything is just fine... @gfwilliams: 100 pts, @candidate: 0 - GAME OVER! ...TL;DR:... the rest of this and following posts.... hold your horses, @asez73... @Konkery, I cannot speak for all details of JS in Espruino, but I get out of this challenge with the basic understanding and applying of With that, @Konkery, you can really beat it in even more than one way...
Above uses just pure basic JS... yes, I know that In fairness, I cannot say wether node.js does it right... or browser or Espruino does it right, because - as said earlier - the caller of the function is not the object that does setup the interval - setInterval(... - it is the underlaying system... and I would not know where this would be specified... so I'm not surprised that that you and I get this error when coding it as in post #1. Finally, @Konkery, I beg you for a favor and edit the conversation (title) to something like ```How do deal with this this in JS when I get error message "xyz not found!" PS - Disclaimer: I could be wrong... would not be the first time... and also not the first time I have to blame myself... for a nice - stingy - laughter about myself! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-02 by opichals An Arrow function is different from a normal one in that its context is not bound and is taken from the scope lexically above it. Here is an article explaining the differences. The code looks as correct ES6 wrt to context use and it should work (I had to fix class inheritance and undeclared variable use in order to run it in Chrome). It looks like an Espruino bug. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-02 by Konkery 1v96.43 |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-02 by Konkery I studied Gordon's article and found no contradictions in his code. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-02 by Konkery @allObjects Does not work:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Robin Sat 2019.03.02
Yes, quite nicely done, and was so in response to issues I had with large classes within modules using I noticed that from #1 above:
as you have quite a few lines of code, are you using that chunk in a module and have Espruima on perhaps? My issues were resolved by turning minification off. (as ES6 reverts to ES5 under some conditions)
Espruino change log Release dates Several enhancements were added between October and January related to ES6 and had me re-flash from 1v94-1v99-2v00-2v01 1v96 has been around for a year and ES6 changes were peppered in current releases. Still in it's infancy. As @opichals #7 most likely is using a recent release and has indicated:
It is quite possible that it is just that, or that all nuances of ES6 are just not implemented yet. @gfwilliams most likely will be the definitive knowledge source here. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by @allObjects @Konkery, did you even try? where is your 'correct' code and the console output? ...because it just works... Here the fixed code (w/ B extending A) with the two presented options for tackling the correct context:
And related console output (PICO w/ Espruino 2v01):
Since there were other bugs in the code - missing parent class reference AND IT WORKS TOO - I assume now that you ran a different code than what you pug in post #1,... and that code is buggy on some other terms...
...and related output:
Conclusion: Everything is just fine... @gfwilliams: 100 pts, @candidate: 0 - GAME OVER |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Robin Sat 2019.03.02 I wish I had as much time as you do @allObjects to try all these situations. ;-) Good catch Line #18 That and; It is most likely 1v96 that is causing the headache as I pointed out in #11 and that @opichals may not have tested using 2v01 as did you. Law of unintended consequences not keeping up on current releases. . . . |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by @allObjects @robin, I find it troubling to make a statement and not providing the prove for it. Agreed, there is not always time for it... but then the attitude helps setting the mending tone... (I know also that times have changed since I went to kindergarten: now, alt-facts seem to become truth when repeated often enough...). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Robin
Ahhhh, a reference to our failed media. . . . Fake News!! While I agree that #10 could have had a bit more substance, It might be that the combination of steps led our candidate to draw an incorrect conclusion, thus not allowing further progress. On occasion, I've been stymied on what I've believed to be an absolute, only to realize the next day there was in fact, another option. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by @allObjects Btw, the media is only the carrier... the feeders sit somewhere else... It is always easier to shoot the messenger than taking responsibilities for the actions. - ...and as for existence of any issue, it needs at least two non-political (or political?) parties that cannot find a solution which actually benefits the non-involved 3rd one, even though it is all about exactly that one. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Konkery Console conclusion at implementation of your code:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Konkery It is similar that in version 1v96.43 it does not work... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Robin Sun 2019.03.03 @Konkery please post the results of typing
I don't have ver 1v96 flashed and, was the following done? Turn off minification |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Konkery Hi, |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Konkery
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by opichals @allObjects @Konkery Unfortunately I did not test it in Espruino before so sorry for pronouncing it a problem. I can confirm now that for the |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by Robin EDIT: Forum update occurred while in edit mode #18 so this entry appears mis-placed #20 #21 #22 Sun 2019.03.03 In reply to #18 I copied the source Turn off minification
I repeated the test with the second example and got the same results. 2v00 and 2v01 a-okay Conclusion: Everything is just fine... @allObjects BONUS points for both working examples!! Attempting to run on 1v94 results in a constructor error (as expected) as this wasn't implemented as of that release. From change log: 1v96 Add ES6 classes and 'super' I'd reflash to the current version. Ahhhh, I see why the reluctance to do so: and/then I'd contact: as Amperka boards are not supported Consider contributing to Espruino with an authentic board purchase for active support, especially for the amount of time utilized in chasing this issue down. Would have had this working right out of the gate. . . . EDIT: Forum update occurred while in edit mode #18 so this entry appears mis-placed #20 #21 #22 |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-03-03 by @allObjects @Konkery, thank your for clarifying the input about version as well as board and build. I apologize for jumping to conclusions with assumptions I did not verify with you before. The main assumption were the version and that you had updated to it after failing with outdated ones. Version 2+ brought really substantial enhancement and extensions. Even though ES6 is mainly syntax sugar, it make code really much more readable when going down the class based oo programming path. You can get there too by some poly filling or by using some frameworks - such as dojo @ dojotoolkit.org did quite consistently and successfully more than a decade ago (from which, btw, dynamic modularization w/ requireJS comes from) - but it is always stays a bit messy. You can RYO by implementing only simple inheritance things using mixins - extensively used by dojo and described, for example, in this Medium.com article from Eric Elliot about Composing Software, hinting relation to software as art. On the other hand, inheritance is quite overrated... because the things in the world do inherit, but not in the narrow sense of class based modeling, but more in the prototype based way. Interestingly this term is use in modeling in scale... model trains, etc. To me this prototype based inheritance means: looks like, but is its very own thing with its very own knowledge and behavior. So in most cases you choose composition over inheritance, because this gives you way more freedom to do things. Another issue with inheritanc vs. mixin/composition in micro controller context is the resources: from a maintenance point of view of supporting a library, class based / prototype based inheritance comes in handy... but at runtime, you rarely have multiple versions of a sensor or alike at that would asks for software to support inheritance for a gain. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2019-03-01 by Konkery
Hi !
an error occurs when you call run() -> this.AsyncFuncA()
Web IDE:
"Uncaught Error: Function "AsyncFuncA" not found!
at line 248 col 6
this.AsyncFuncA();
^
in function called from system"
how can this be fought ?
Beta Was this translation helpful? Give feedback.
All reactions