-
Heyo, I'm migrating from My pain point is this const runtime = createWorkletRuntime();
// Create Worklet Function once
const worklet = runOnRuntime(runtime, (data: object) => {
"worklet";
const res = doSomeWork(data); // also a worklet, duh
console.log(res); // res logs as expected
return res;
});
// Call from JS and use the returned value
const returned = await worklet(someData);
console.log(res) // => undefined 😡 As I understand, the only way to accomplish returning data to JS currently is to pass a callback function that would assign to a pre-existing object on the JS runtime. Then I would need to use some message passing pattern to know when the worklet has returned. Not very ergonomic. Am I missing anything? I see that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @alexphl, as you mentioned, If you want to return a value with a promise, the best approach at the moment is to manually make a promise, i.e.: import {
createWorkletRuntime,
scheduleOnRuntime,
scheduleOnRN,
} from 'react-native-worklets';
const workletRuntime = createWorkletRuntime({ name: 'MyRuntime' });
const promise = new Promise<string>((resolve) => {
scheduleOnRuntime(workletRuntime, () => {
'worklet';
console.log('Resolving promise on worklet runtime');
scheduleOnRN(resolve, 'Promise resolved on worklet runtime');
});
});
promise
.then((message) => {
console.log(message);
})
.catch(() => {}); |
Beta Was this translation helpful? Give feedback.
Hi @alexphl, as you mentioned,
runOnRuntimeAsync
is not yet implemented.If you want to return a value with a promise, the best approach at the moment is to manually make a promise, i.e.: