Js2Py icon indicating copy to clipboard operation
Js2Py copied to clipboard

Async / Await

Open psytron opened this issue 3 years ago • 5 comments

How to enable async await?

PromiseLib = js2py.require('my-feed-js')
x  = await PromiseLib.fetchData() 

psytron avatar May 20 '21 02:05 psytron

please, that would be so helpful!

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

MagMueller avatar Apr 29 '22 16:04 MagMueller

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.

worstperson avatar Apr 30 '22 05:04 worstperson

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?

MagMueller avatar Apr 30 '22 11:04 MagMueller

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.

worstperson avatar Apr 30 '22 17:04 worstperson

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())

milahu avatar Feb 20 '24 09:02 milahu