Skip to content

Tips on how to run async code #7

@simonw

Description

@simonw

Steps to reproduce

>>> import py_mini_racer
>>> context = py_mini_racer.MiniRacer()
>>> result = context.eval("""
... async function pretendToDelay() {
...     return new Promise(resolve => {
...         setTimeout(() => resolve('Data loaded!'), 1000);
...     });
... }
... pretendToDelay();
... """)
>>> print(result, result.__dict__)
<py_mini_racer.py_mini_racer.JSObject object at 0x100ec6620> {'id': 965435}

That code to paste:

import py_mini_racer
context = py_mini_racer.MiniRacer()
result = context.eval("""
async function pretendToDelay() {
    return new Promise(resolve => {
        setTimeout(() => resolve('Data loaded!'), 1000);
    });
}
pretendToDelay();
""")
print(result, result.__dict__)

I tried using await pretendToDelay(); on the last line instead and got this:

py_mini_racer.py_mini_racer.JSParseException: SyntaxError: await is only valid in async functions and the top level bodies of modules

Expected behavior

I'm trying to figure out how to return the result from resolving that promise.

Assuming there's a pattern for doing this, it would be great if the test suite and documentation described it!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions