Js2Py
Js2Py copied to clipboard
Async / Await
How to enable async await?
PromiseLib = js2py.require('my-feed-js')
x = await PromiseLib.fetchData()
please, that would be so helpful!
Is there any possibility to run async function? I am getting a syntax error
This depends on ES6 Promise support before it can be added: https://github.com/PiotrDabkowski/Js2Py/issues/273
The await keyword looks like it's blocking, so we'd probably need to implement threading for it to work.
This depends on ES6 Promise support before it can be added: #273
The await keyword looks like it's blocking, so we'd probably need to implement threading for it to work.
Can We help you somehow with it, to make that happen?
I suppose by becoming so motivated to get your code running that you pull up the specifications and produce patches to try and implement those features. Threading in particular is a huge open question for me. I have so far put off any effort in researching it personally.
I have looked into adding Promise support, but none of the code I want to run uses it. Makes investing time into it a tough sell, especially given that it's more or less out-of-scope for an ecmascript 5.1 interpreter.
example: sleep should be translated to asyncio.sleep
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
await sleep(100);
}
main();
import asyncio
async def main():
await asyncio.sleep(0.1)
asyncio.run(main())
chatGPT response
import asyncio
async def sleep(seconds):
await asyncio.sleep(seconds / 1000)
async def main():
await sleep(100)
asyncio.run(main())