Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS Promise partial support #769

Closed
karray opened this issue Oct 16, 2020 · 4 comments
Closed

JS Promise partial support #769

karray opened this issue Oct 16, 2020 · 4 comments

Comments

@karray
Copy link
Contributor

karray commented Oct 16, 2020

It seems that js promises only works partially.

Can't create a promise from Python:

from js import window

window.Promise(lambda resolve: resolve())
# TypeError: undefined is not a promise

promise.finally not working:

// JS
window.fetch('https://github.com/iodide-project/pyodide').then(resp => console.log(resp.status)).finally(()=>console.log('Done'))
#python
from js import window

window.fetch('https://github.com/iodide-project/pyodide').then(lambda resp: print(resp.status)).finally(lambda: print('done'))
SyntaxError: invalid syntax

    at _hiwire_throw_error (pyodide.asm.js:8)
    at _pythonexc2js (pyodide-cdn2.iodide.io/v0.15.0/full/pyodide.asm.wasm:wasm-function[358]:0xfc9e1)
    at __findImports (pyodide-cdn2.iodide.io/v0.15.0/full/pyodide.asm.wasm:wasm-function[386]:0xfe795)
    at Object.Module.__findImports (pyodide.asm.js:8)
    at Object.Module.runPythonAsync (pyodide.asm.js:8)
    at evaluatePython (index.html:41)
    at HTMLButtonElement.onclick (index.html:17)
@rth
Copy link
Member

rth commented Oct 16, 2020

For Promise the error is also more explicit in Firefox,

TypeError: calling a builtin Promise constructor without new is forbidden

Here, unlike #768 importing from js works fine,

>>> from js import Promise
>>> Promise.new(lambda resolve, err: resolve())

About finally, unfortunately it's a reserved key word in Python, so it cannot be a method name. So for instance,

class A:
    def finally(self):
        return 0

produces,

    def finally(self):
        ^
SyntaxError: invalid syntax

in Python (even outside Pyodide). Let's see if anyone has ideas how to address this on the CPython side (https://stackoverflow.com/questions/64396988).

@hoodmane
Copy link
Member

PR #880 wraps the promise in a Future so we could expose that API, but it's a bit different, and a bit less usable, and doesn't have an equivalent to finally.
We should expose the finally method on the proxied future at a slightly different name. eval_code should be updated to look for syntax errors triggered by finally and tell people about the moved API.

@hoodmane
Copy link
Member

@dalcde Do you have any opinion about what name we should use for the promise finally method on JsProxy?

@hoodmane
Copy link
Member

Closed by #1334.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants