From 76dfa9ba8555ba8eea9692591d948fd9cfd88d7a Mon Sep 17 00:00:00 2001 From: Website Deployment Script Date: Wed, 5 Apr 2017 19:32:11 +0000 Subject: [PATCH] Updated docs for next --- releases/next/docs/native-modules-ios.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releases/next/docs/native-modules-ios.html b/releases/next/docs/native-modules-ios.html index 492802ffd78..d9b1f74d1b5 100644 --- a/releases/next/docs/native-modules-ios.html +++ b/releases/next/docs/native-modules-ios.html @@ -54,7 +54,7 @@ CalendarManager.this.setState({events: events}); } })

A native module should invoke its callback exactly once. It's okay to store the callback and invoke it later. This pattern is often used to wrap iOS APIs that require delegates - see RCTAlertManager for an example. If the callback is never invoked, some memory is leaked. If both onSuccess and onFail callbacks are passed, you should only invoke one of them.

If you want to pass error-like objects to JavaScript, use RCTMakeError from 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:

RCT_REMAP_METHOD(findEvents, - resolver:(RCTPromiseResolveBlock)resolve + findEventsWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { NSArray *events = ...