PiotrDabkowski / Js2Py

JavaScript to Python Translator & JavaScript interpreter written in 100% pure Python🚀 Try it online:
http://piter.io/projects/js2py
MIT License
2.45k stars 259 forks source link

Async / Await #250

Open psytron opened 3 years ago

psytron commented 3 years ago

How to enable async await?

PromiseLib = js2py.require('my-feed-js')
x  = await PromiseLib.fetchData() 
MagMueller commented 2 years ago

please, that would be so helpful!

Is there any possibility to run async function? I am getting a syntax error

worstperson commented 2 years ago

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.

MagMueller commented 2 years ago

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?

worstperson commented 2 years ago

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.

milahu commented 6 months ago

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 ```py import asyncio async def sleep(seconds): await asyncio.sleep(seconds / 1000) async def main(): await sleep(100) asyncio.run(main()) ```