Supporting ES6 Promises

165 views
Skip to first unread message

pi

unread,
Aug 21, 2015, 8:34:39 AM8/21/15
to brython
Does Brython support ES6 promises?


I'm getting a strange code error.

My code effectively does this:

def myMIDIMessagehandler(event):
statusByte, *data = event.data
:

def onSuccess( access ):
:
for v in inputs.values():
print(v.name)
v.onmidimessage = myMIDIMessagehandler 

window.navigator.requestMIDIAccess().then( onSuccess, onError ) // (1)

:
print( "Ready!" )

i.e. It sets up MIDI so that myMIDIMessagehandler fires whenever an event is received from one of my midi keyboards.

(1) is an ES6 Promise.  That means of execution should continue Immediately.

However, "Ready!" does not print.

When I generate an event from my midi keyboard, I get:

Uncaught Error: Traceback (most recent call last):

 module __main__ line 236

   print( "Ready!" )

 module __main__ line 85

    statusByte, *data = event.data

NameError: 'data'


That seems to be suggesting that line 236 is only getting encountered when the event occurs.

It's a strange error,  because it seems to be pointing in two different places at once, and both lines are legal Python (I've checked in Brython console).


My guess is that Brython has not implemented promises yet, and it is some kind of coincidence that this partially works rather than being entirely broken.


Could we hope for Promise support in Brython in the near future?


π

Olemis Lang

unread,
Aug 21, 2015, 12:09:42 PM8/21/15
to bry...@googlegroups.com
Not explicitly . I might take a look into this once I try to implement
#210 [1]_ but something can be done sooner than that in the context of
#242 [2]_

> I'm getting a strange code error.
>
[...]
>
> My guess is that Brython has not implemented promises yet, and it is some
> kind of coincidence that this partially works rather than being entirely
> broken.
>
> Could we hope for Promise support in Brython in the near future?
>
>

I guess so , because this will be the main building block necessary to
implement many other open tickets . It's just a matter of us having
time to get things done .

.. [1] https://github.com/brython-dev/brython/issues/210

.. [2] https://github.com/brython-dev/brython/issues/242

--
Regards,

Olemis - @olemislc

Apache™ Bloodhound contributor
http://issues.apache.org/bloodhound
http://blood-hound.net

Brython committer
http://brython.info
http://github.com/brython-dev/brython

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:

Olemis Lang

unread,
Aug 21, 2015, 12:13:25 PM8/21/15
to bry...@googlegroups.com

pi

unread,
Aug 21, 2015, 12:53:17 PM8/21/15
to brython
I guess so , because this will be the main building block necessary to
implement many other open tickets . It's just a matter of us having
time to get things done .

Does anyone fund you guys?  To me this appears to be valuable work -- Python has enough merit over JS (even over ES6) that it makes a lot of sense to work this way.  I would've thought it would be in the interest of big companies e.g. Google to fund, if for no other reason surely it would be in their best interests. (e.g. Google make extensive use of Python internally) -- surely any company that is paying JS devs would significantly increase their efficiency within a couple of years of switching to Python.  I think debugging is the the main problem where JS still wins out, the fact that e.g. Chrome has an integrated debugging environment. Although I'm not finding that as much of a problem as I thought I would.

It looks like my work hinges on the ES6 Promise, so I'm subscribing to the GitHub thread eagerly awaiting further notification.

Maybe in the meanwhile I can just move the single promise line into a JavaScript function, which I call from Brython.

π

pi

unread,
Aug 22, 2015, 6:43:39 AM8/22/15
to brython
I'm completely stuck on this one.

I decided to move the promise out back into JavaScript:

create.html
<head>
<script>
function js_requestMIDIAccess( onSuccess, onError ) {
window.navigator.requestMIDIAccess().then( onSuccess, onError );
}
</script>
<script src="/lib/bry_edge/brython.js"></script>
  <script type="text/python3" src="./create.py"></script>
</head>

<body onload="console.log('a'); brython({debug:1, cache:'none'}); console.log('b');" > 
:

create.js
:
def myMIDIMessagehandler(event):
statusByte, *data_ = event.data
:

def onSuccess( access ):
for v in access.inputs.values():
v.onmidimessage = myMIDIMessagehandler 

:
#window.navigator.requestMIDIAccess().then( onSuccess, onError )
window.js_requestMIDIAccess( onSuccess, onError )
:
print( "Ready!" )

And it still gives the same error when I press a key on my midi keyboard.

This makes no sense to me,  because now JavaScript is handling the promise.

Whichever way I turn this,  I cannot make sense of it.

Can anyone suggest a move?

π

pi

unread,
Aug 23, 2015, 9:31:04 AM8/23/15
to brython
Looks like I found the bug: https://groups.google.com/forum/?hl=en#!topic/brython/OnxUzMBfiU8

(I started a new thread, as it is unconnected with promises)

π

pi

unread,
Aug 24, 2015, 10:16:34 AM8/24/15
to brython
Just to confirm, this was never an issue with promises. The issue was the bug reported in the previous post which has since been fixed.

So now the original code works.

I should have put a print statement on the first line of my callback, so this is is a debugging fail on my part. Sorry!

π
Reply all
Reply to author
Forward
0 new messages