margelo / react-native-worklets-core

🧵 A library to run JS functions ("Worklets") on separate Threads
https://margelo.io
MIT License
552 stars 37 forks source link

Async/Await in Worklet Functions #109

Open robnewton opened 1 year ago

robnewton commented 1 year ago

Is it possible to use async/await within Worklets?

I'm trying to make calls to asynchronous functions outside of the function tagged with the worklet keyword? As a simple example, is it possible to use the Expo FileSystem async functions like copyFileAsync() from within a worklet?

When tried, I get errors like this:

TypeError: Cannot assign to read only property 'message' of object 'SyntaxError: unknown: Unexpected reserved word 'yield'.
bglgwyng commented 1 year ago

You can't do asynchronous things within worklets since they don't run the event loop as far as I know.

However, I didn't expect that async/await syntax is not supported in worklets. The error message indicates that worklets don't support generator syntax which is emitted as a transpilation result of async/await.

robnewton commented 11 months ago

That makes sense but I was hoping it would just execute synchronously. In this scenario I don't need it to be asynchronous since it's in another thread, I just need to call something which relies on that syntax.