mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Added docs on using Promisesin Native Modules
This commit is contained in:
1 parent
5f226cf4c5
commit
fdca4224bc
2 files changed
+94
-2
No files matched your search
@@ -173,6 +173,42 @@ A native module is supposed to invoke its callback only once. It can, however, s
|
||||
|
||||
If you want to pass error-like objects to JavaScript, use `RCTMakeError` from [`RCTUtils.h`](https://github.com/facebook/react-native/blob/master/React/Base/RCTUtils.h). Right now this just passes an Error-shaped dictionary to JavaScript, but we would like to automatically generate real JavaScript `Error` objects in the future.
|
||||
|
||||
## Promises
|
||||
|
||||
Native modules can also fulfill a promise, which can simplify your code, especially when using ES2016's `async/await` syntax. When the last parameters of a bridged native method are an `RCTPromiseResolveBlock` and `RCTPromiseRejectBlock`, its corresponding JS method will return a JS Promise object.
|
||||
|
||||
Refactoring the above code to use a promise instead of callbacks looks like this:
|
||||
|
||||
```objective-c
|
||||
RCT_REMAP_METHOD(findEvents,
|
||||
resolver:(RCTPromiseResolveBlock)resolve,
|
||||
rejecter:(RCTPromiseRejectBlock)reject))
|
||||
{
|
||||
NSArray *events = ...
|
||||
if (events) {
|
||||
resolve(events);
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The JavaScript counterpart of this method returns a Promise. This means you can use the `await` keyword within an async function to call it and wait for its result:
|
||||
|
||||
```js
|
||||
async function updateEvents() {
|
||||
try {
|
||||
var events = await CalendarManager.findEvents();
|
||||
|
||||
this.setState({ events });
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
updateEvents();
|
||||
```
|
||||
|
||||
## Threading
|
||||
|
||||
The native module should not have any assumptions about what thread it is being called on. React Native invokes native modules methods on a separate serial GCD queue, but this is an implementation detail and might change. The `- (dispatch_queue_t)methodQueue` method allows the native module to specify which queue its methods should be run on. For example, if it needs to use a main-thread-only iOS API, it should specify this via:
|
||||
|
||||
Reference in new issue
Block a user