We have a React Native Module, with iOS and Android support. At some point, the native iOS and Android components will pass a parameter to JavaScript and ask for info.
We saw in the docs that we can use Callbacks. Knowing that callbacks are functions, it can takes parameters and return values.
For simplicity, I'll share one method from the React Native docs to demonstrate our problem.
// iOS
RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)callback) {
NSArray *events = ...
id result = callback(@[[NSNull null], events]);
// ~~~^~~~
// This is not allowed, RCTResponseSenderBlock returns void
}
// Android
@ReactMethod
public void findEvents(Callback errorCallback, Callback successCallback) {
Event events[] = ...
try {
Object result = successCallback.invoke(events);
// ~~~~^~~~~
// This is not allowed, Callback returns void
} catch (IllegalViewOperationException e) {
errorCallback.invoke(e.getMessage());
}
}
In this example, we pass events, and ask for result from JavaScript. In JavaScript, code should be:
CalendarManager.findEvents((error, events) => {
if (error) {
console.error(error);
} else {
return { "success": true } // we want to receive this in Native code.
}
});
We also looked at Promises and Event Emitters. But none of them designed for returns.