You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[RFC][Bridge] Add support for JS async functions, backed by RCT_EXPORT_PROMISE
Adds support for JS async methods and helps guide people writing native modules w.r.t. the callbacks. With this diff, on the native side you write:
```objc
RCT_EXPORT_PROMISE(getValueAsync:(NSString *)key)
{
NSError *error = nil;
id value = [_nativeDataStore valueForKey:key error:&error];
// "resolve" and "reject" are automatically defined blocks that take
// any object (nil is OK) and an NSError, respectively
if (!error) {
resolve(value);
} else {
reject(error);
}
}
```
On the JS side, you can write:
```js
var {DemoDataStore} = require('react-native').NativeModules;
DemoDataStore.getValueAsync('sample-key').then((value) => {
console.log('Got:', value);
}, (error) => {
console.error(error);
// "error" is an Error object whose message is the NSError's description.
// The NSError's code and domain are also set, and the native trace is
// available under
});
```
And if you take a time machine or use Babel w/stage 1, you can write:
```js
try {
var value = await DemoDataStore.getValueAsync('sample-key');
console.log('Got:', value);
} catch (error) {
console.error(error);
}
```
For now the macro is defined as RCT_EXPORT_PROMISE_EXPERIMENTAL since I'd like to merge this and get real-world feedback on the API before committing to it.
0 commit comments