Create a Windows module for React Native with asynchronous code in C#

Published Jun 08 2020 08:39 AM 3,560 Views
Microsoft

We have already explored on this blog the opportunity to create native Windows modules for React Native. Thanks to these modules, you are able to surface native Windows APIs to JavaScript, so that you can leverage them from a React Native application running on Windows. Native modules are one of the many ways supported by React Native to build truly native applications. Despite you're using web technologies, you are able to get access to native features of the underlying platform, like notifications, storage, geolocation, etc.

 

In the previous post we have learned the basics on how to create a module in C# and how to register it in the main application, so that the C# functions are exposed as JavaScript functions. As such, in this post I won't go again into the details on how to build a module and how to register it in the Windows implementation of the React Native app. However, recently, I came across a blocker while working on a customer's project related to this scenario. My post (like the official documentation) was leveraging synchronous methods. In my scenario, instead, I needed to use the Geolocation APIs provided by Windows 10, which are asynchronous and based on the async / await pattern.

 

As such, I've started to build my module using the standard async / await pattern:

 

namespace GeolocationModule
{
    [ReactModule]
    class GeolocationModule
    {
        [ReactMethod("getCoordinates")]
        public async Task<string> GetCoordinates()
        {
            Geolocator geolocator = new Geolocator();
            var position = await geolocator.GetGeopositionAsync();

            string result = $"Latitude: {position.Coordinate.Point.Position.Latitude} - Longitude: {position.Coordinate.Point.Position.Longitude}";

            return result;
        }
    }
}

This is the traditional implementation of this pattern:

  • The GetCoordinates() method is marked with the async keyword
  • The GetCoordinates() method returns Task<T>, where T is the type of the result we want to return (in our case, its' a string)
  • When we call an asynchronous API in the body (the GetGeopositionAsync() method exposed by the Geolocator object), we add the await prefix.

Then, in the React Native portion, I've created a wrapper for this module using the NativeModules APIs, exactly like I did in my previous sample:

 

export const getCoordinates = () => {
  return new Promise((resolve, reject) => {
    NativeModules.GeolocationModule.getCoordinates(function(result, error) {
      if (error) {
        reject(error);
      }
      else {
        resolve(result);
      }
    })
  })
}

Once I've connected all the dots and launched the React Native application, however, I was greeted with an unpleasant surprise. As soon as I hit the button which was invoking the getCoordinates() function, the application was crashing.

 

Thanks to a chat with Vladimir Morozov from the React Native team, it turned out that React Native doesn't support methods which return a Task<T>. In order to work properly, they must return void. How is it possible to achieve this goal and, at the same time, being able to keep calling asynchronous APIs, like the ones exposed by the Geolocator class? Thanks to Vladimir who put me on the right track, the solution is easy. Let's explore the options we have.

Use promises

When it comes to JavaScript, I'm a big fan of promises since they enable a syntax which is very similar to the C# one. When an asynchronous method returns a Promise, you can simply mark the function with the async keyword and add the await prefix before calling the asynchronous function, like in the following sample:

 

const getData = async () => {
    var result = await fetch ('this-is-a-url');
    //do something with the result
}

Let's start to see how we can build our module so that it can return a promise. It's easy, thanks to the IReactPromise interface included in the React Native implementation for Windows. Let's see some code first:

 

using Microsoft.ReactNative.Managed;
using System;
using Windows.Devices.Geolocation;

namespace GeolocationModule
{
    [ReactModule]
    class GeolocationModule
    {
        [ReactMethod("getCoordinatesWithPromise")]
        public async void GetCoordinatesWithPromise(IReactPromise<string> promise)
        {
            try
            {
                Geolocator geolocator = new Geolocator();
                var position = await geolocator.GetGeopositionAsync();

                string result = $"Latitude: {position.Coordinate.Point.Position.Latitude} - Longitude: {position.Coordinate.Point.Position.Longitude}";

                promise.Resolve(result);
            }
            catch (Exception e)
            {
                promise.Reject(new ReactError { Exception = e });
            }
        }
    }
}

As first step, we need to declare the method as async void, in order to be compliant with the React Native requirements. To handle the asynchronous nature of the method, the key component is the requested parameter, which type is IReactPromise<T>, where T is the type of the value we want to return. In my scenario I want to return a string with the full coordinates, so I'm using the IReactPromise<string> type.

 

Inside the method we can start writing our code like we would do in a traditional Windows application. Since we have marked the method with the async keyword, we can just call any asynchronous API (like the GetGeopositionAsync() one exposed by the Geolocator class) by adding the await prefix.

 

Once we have completed the work and we have obtained the result we want to return, we need to pass it to the Resolve() method exposed by the IReactPromise parameter. The method expects a value which type is equal to T. In my case, for example, we would get an error if we try to pass anything but a string.

 

In case something goes wrong, instead, we can use the Reject() method to surface the error to the React Native application, by creating a new ReactError object. You can customize it with different parameters, like Code, Message and UserInfo. In my case, I just want to raise the whole exception, so I simply set the Exception property exposed by my try / catch block.

 

That's it! Now we can easily define a function in our React Native code that, by using the NativeModules API and a Promise, can invoke the C# method we have just created, as in the following sample:

 

const getCoordinatesWithPromise = async () => {
  var coordinates = await NativeModules.GeolocationModule.getCoordinatesWithPromise();
  console.log(coordinates);
}

Since we're using a Promise, the syntax is the same we have seen in the previous sample. We mark the function with the async keyword and we call the getCoordinatesWithPromise() method with the await prefix. Thanks to the Promise we can be sure that, when we invoke the console.log() function, the coordinates property has been properly set. The native module is exposed through the NativeModules APIs and the GeolocationModule object, which is the name of the C# class we have created (if you remember, we have marked it with the [ReactModule] attribute).

 

Use callbacks

I really much prefer the syntax offered by Promises but if, by any chance, you prefer to use callbacks, we got you covered! You can use, in fact, a slightly different syntax, based on the Action<T> object, to expose your native C# method as a callback. Let's see the implementation:

 

using Microsoft.ReactNative.Managed;
using System;
using Windows.Devices.Geolocation;

namespace GeolocationModule
{
    [ReactModule]
    class GeolocationModule
    {
        [ReactMethod("getCoordinatesWithCallback")]
        public async void GetCoordinatesWithCallback(Action<string> resolve, Action<string> reject)
        {
            try
            {
                Geolocator geolocator = new Geolocator();
                var position = await geolocator.GetGeopositionAsync();

                string result = $"Latitude: {position.Coordinate.Point.Position.Latitude} - Longitude: {position.Coordinate.Point.Position.Longitude}";

                resolve(result);
            }
            catch (Exception e)
            {
                reject(e.Message);
            }
        }
    }
}

In this case, as parameters of the method, we are passing two objects which type is Action<T>. The first one will be used when the method completes successfully, the second one when instead something goes wrong. In this case, T is a string in both cases. In case of success, we want to return the usual string with the full coordinates; in case of failure, we want to return the message of the exception.

 

The rest of the code is similar to the one we have written before. The only difference is that, when we have achieved our result, we just pass it to the resolve() function; when something goes wrong, instead, we call the reject() function. The main difference compared to the previous approach is that Action<T> isn't a structured object like the IReactNative<T> interface. As such, it doesn't support to pass the full exception but, in this case, we choose to pass only the Message property of the exception.

 

That's if from the C# side. Let's move to the JavaScript one. In this case, being a callback, we can't use anymore the async and await keywords, but we need to pass to the method two functions: one will be called when the operation is successful, one when we get an error.

 

const getCoordinatesWithCallback = () => {
  NativeModules.GeolocationModule.getCoordinatesWithCallback( (result) => { console.log(result); }, (error) => console.log(error));
}

 

Passing parameters

In both scenarios, if we need to pass any parameter from the JavaScript code to the C# one we can just add them before the IReactPromise<T> or the Action<T> ones. For example, let's say we want to set the desired accuracy of the Geolocator object with a value in meters, passed from the React Native app. We can just define the method like this:

 

[ReactMethod("getCoordinatesWithPromise")]
public async void GetCoordinatesWithPromise(uint meters, IReactPromise <string> promise)
{
    try
    {
        Geolocator geolocator = new Geolocator();
        geolocator.DesiredAccuracyInMeters = meters;
        
       var position = await geolocator.GetGeopositionAsync();

        string result = $"Latitude: {position.Coordinate.Point.Position.Latitude} - Longitude: {position.Coordinate.Point.Position.Longitude}";

        promise.Resolve(result);
    }
    catch (Exception e)
    {
        promise.Reject(new ReactError { Exception = e });
    }
}

Then, from JavaScript, we just need to pass the value of the parameter when we invoke the getCoordinatesWithPromise() function:

 

const getCoordinatesWithPromise = async () => {
  var coordinates = await NativeModules.GeolocationModule.getCoordinatesWithPromise(15);
  console.log(coordinates);
}

Of course, you can use the same pattern if you prefer to leverage the callback approach.

 

Wrapping up

On this blog we have already learned how to build native C# modules for React Native so that, as a developer, you can access native Windows APIs from JavaScript. However, in that sample we leveraged only synchronous APIs. The reality, however, is that most of the Windows 10 APIs are implemented with the asynchronous pattern. Unfortunately, as soon as you start using them, you will face errors and crashes, since React Native isn't able to properly understand the traditional async / await pattern, based on the Task object. Thanks to the React Native team I've been pointed into the right direction, by leveraging the IReactNative interface (in case you want to enable asynchronous APIs with promises) or the Action object (in case you want to leverage the more traditional callback approach).

 

Regardless of the approach you prefer, you'll be able to achieve your goal of leveraging asynchronous Windows 10 APIs from your React Native application running on Windows:

 

FinalApp.png

3 Comments
Visitor

Hi Matteo,

 

Very good documentation and your blog posts helped me a lot, thank you very much.

 

But, I have problems about calling a javascript method from C# code, for example I catch keydown event on Window.Current.CoreWindow.KeyDown and try to send string value of the pressed key to js side using parameter args.VirtualKey.ToString().

When I call the CallJSFunction from ReactContext, react context is always null and I can't call js function.

 

This approach works in debug mode in visual studio 2019, I can send the pressed key as string from c# and get it at js side.

But when in release mode, react context is always null.

Should I run metro bundler when getting a release build too? Metro Bundler seems like working when getting release build in output window in Visual Studio.

 

Can you please help me about this issue?

 

Thank you,

Hamza

Microsoft

Hello @hamzasari make sure that, in your module, you have a method decorated with the [ReactInitializer] attribute, like in the following sample:

 

private ReactContext _context;

[ReactInitializer]
public void Initialize(ReactContext context)
{
     _context = context;
}

 

Otherwise, the ReactContext will be always null.

I hope it helps!

Visitor

Hello @Matteo Pagani ,

 

I'm sorry I couldn't reply earlier and thank you for your suggestion.

 

I did what you suggested and this works perfectly in debug mode but when I build my application with release mode and run it, it immediately crashes and closes. Then I tried to debug in release mode with Visual Studio 2019 Community Edition, in the KeyDown event context object seems to be null, so I cannot get which key is pressed and cannot send the pressed keycode to javascript side.

 

For now, I'm using default react native focus navigation feature, without catching pressed keys and sending them from native side to javascript side. And it seems ok for directions, A and B buttons. Without sending pressed key code to javascript side, I cannot use other controller buttons for custom actions, but I think I will use default feature for now and leave the rest. If this issue is fixed or added as a feature in the future, I will give it a try.

 

Thank you again for your help.

%3CLINGO-SUB%20id%3D%22lingo-sub-1448692%22%20slang%3D%22en-US%22%3ECreate%20a%20Windows%20module%20for%20React%20Native%20with%20asynchronous%20code%20in%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1448692%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-dev-appconsult%2Fbuilding-a-react-native-module-for-windows%2Fba-p%2F1067893%22%20target%3D%22_blank%22%3EWe%20have%20already%20explored%20on%20this%20blog%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ethe%20opportunity%20to%20create%20native%20Windows%20modules%20for%20React%20Native.%20Thanks%20to%20these%20modules%2C%20you%20are%20able%20to%20surface%20native%20Windows%20APIs%20to%20JavaScript%2C%20so%20that%20you%20can%20leverage%20them%20from%20a%20React%20Native%20application%20running%20on%20Windows.%20Native%20modules%20are%20one%20of%20the%20many%20ways%20supported%20by%20React%20Native%20to%20build%20truly%20native%20applications.%20Despite%20you're%20using%20web%20technologies%2C%20you%20are%20able%20to%20get%20access%20to%20native%20features%20of%20the%20underlying%20platform%2C%20like%20notifications%2C%20storage%2C%20geolocation%2C%20etc.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-dev-appconsult%2Fbuilding-a-react-native-module-for-windows%2Fba-p%2F1067893%22%20target%3D%22_blank%22%3EIn%20the%20previous%20post%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewe%20have%20learned%20the%20basics%20on%20how%20to%20create%20a%20module%20in%20C%23%20and%20how%20to%20register%20it%20in%20the%20main%20application%2C%20so%20that%20the%20C%23%20functions%20are%20exposed%20as%20JavaScript%20functions.%20As%20such%2C%20in%20this%20post%20I%20won't%20go%20again%20into%20the%20details%20on%20how%20to%20build%20a%20module%20and%20how%20to%20register%20it%20in%20the%20Windows%20implementation%20of%20the%20React%20Native%20app.%20However%2C%20recently%2C%20I%20came%20across%20a%20blocker%20while%20working%20on%20a%20customer's%20project%20related%20to%20this%20scenario.%20My%20post%20(like%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fmicrosoft.github.io%2Freact-native-windows%2Fdocs%2Fnative-modules%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Eofficial%20documentation%3C%2FA%3E)%20was%20leveraging%20synchronous%20methods.%20In%20my%20scenario%2C%20instead%2C%20I%20needed%20to%20use%20the%20Geolocation%20APIs%20provided%20by%20Windows%2010%2C%20which%20are%20asynchronous%20and%20based%20on%20the%20async%20%2F%20await%20pattern.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20such%2C%20I've%20started%20to%20build%20my%20module%20using%20the%20standard%20async%20%2F%20await%20pattern%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Enamespace%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGeolocationModule%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%5B%3CSPAN%20class%3D%22hljs-meta%22%3EReactModule%3C%2FSPAN%3E%5D%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eclass%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGeolocationModule%3C%2FSPAN%3E%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%5B%3CSPAN%20class%3D%22hljs-meta%22%3EReactMethod(%3CSPAN%20class%3D%22hljs-meta-string%22%3E%22getCoordinates%22%3C%2FSPAN%3E)%3C%2FSPAN%3E%5D%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Easync%3C%2FSPAN%3E%20Task%26lt%3B%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%26gt%3B%20%3CSPAN%20class%3D%22hljs-title%22%3EGetCoordinates%3C%2FSPAN%3E()%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Geolocator%20geolocator%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Geolocator()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20position%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20geolocator.GetGeopositionAsync()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20result%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%24%22Latitude%3A%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7Bposition.Coordinate.Point.Position.Latitude%7D%3C%2FSPAN%3E%20-%20Longitude%3A%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7Bposition.Coordinate.Point.Position.Longitude%7D%3C%2FSPAN%3E%22%3C%2FSPAN%3E%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ereturn%3C%2FSPAN%3E%20result%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThis%20is%20the%20traditional%20implementation%20of%20this%20pattern%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThe%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGetCoordinates()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20is%20marked%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Easync%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ekeyword%3C%2FLI%3E%0A%3CLI%3EThe%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGetCoordinates()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20returns%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ETask%3CT%3E%3C%2FT%3E%3C%2FCODE%3E%2C%20where%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ET%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eis%20the%20type%20of%20the%20result%20we%20want%20to%20return%20(in%20our%20case%2C%20its'%20a%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Estring%3C%2FCODE%3E)%3C%2FLI%3E%0A%3CLI%3EWhen%20we%20call%20an%20asynchronous%20API%20in%20the%20body%20(the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGetGeopositionAsync()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20exposed%20by%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGeolocator%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject)%2C%20we%20add%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eawait%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eprefix.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThen%2C%20in%20the%20React%20Native%20portion%2C%20I've%20created%20a%20wrapper%20for%20this%20module%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ENativeModules%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EAPIs%2C%20exactly%20like%20I%20did%20in%20my%20previous%20sample%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eexport%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20getCoordinates%20%3D%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-params%22%3E()%3C%2FSPAN%3E%20%3D%26gt%3B%3C%2FSPAN%3E%20%7B%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ereturn%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-built_in%22%3EPromise%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-function%22%3E(%3CSPAN%20class%3D%22hljs-params%22%3Eresolve%2C%20reject%3C%2FSPAN%3E)%20%3D%26gt%3B%3C%2FSPAN%3E%20%7B%0A%20%20%20%20NativeModules.GeolocationModule.getCoordinates(%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Efunction%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3Eresult%2C%20error%3C%2FSPAN%3E)%20%3C%2FSPAN%3E%7B%0A%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%3C%2FSPAN%3E%20(error)%20%7B%0A%20%20%20%20%20%20%20%20reject(error)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eelse%3C%2FSPAN%3E%20%7B%0A%20%20%20%20%20%20%20%20resolve(result)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D)%0A%20%20%7D)%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EOnce%20I've%20connected%20all%20the%20dots%20and%20launched%20the%20React%20Native%20application%2C%20however%2C%20I%20was%20greeted%20with%20an%20unpleasant%20surprise.%20As%20soon%20as%20I%20hit%20the%20button%20which%20was%20invoking%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EgetCoordinates()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction%2C%20the%20application%20was%20crashing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20to%20a%20chat%20with%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.linkedin.com%2Fin%2Fvmoroz%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EVladimir%20Morozov%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efrom%20the%20React%20Native%20team%2C%20it%20turned%20out%20that%20React%20Native%20doesn't%20support%20methods%20which%20return%20a%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ETask%3CT%3E%3C%2FT%3E%3C%2FCODE%3E.%20In%20order%20to%20work%20properly%2C%20they%20must%20return%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Evoid%3C%2FCODE%3E.%20How%20is%20it%20possible%20to%20achieve%20this%20goal%20and%2C%20at%20the%20same%20time%2C%20being%20able%20to%20keep%20calling%20asynchronous%20APIs%2C%20like%20the%20ones%20exposed%20by%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGeolocator%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eclass%3F%20Thanks%20to%20Vladimir%20who%20put%20me%20on%20the%20right%20track%2C%20the%20solution%20is%20easy.%20Let's%20explore%20the%20options%20we%20have.%3C%2FP%3E%0A%3CH3%20id%3D%22use-promises%22%20id%3D%22toc-hId-1171375469%22%20id%3D%22toc-hId-1171375469%22%3EUse%20promises%3C%2FH3%3E%0A%3CP%3EWhen%20it%20comes%20to%20JavaScript%2C%20I'm%20a%20big%20fan%20of%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FPromise%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Epromises%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Esince%20they%20enable%20a%20syntax%20which%20is%20very%20similar%20to%20the%20C%23%20one.%20When%20an%20asynchronous%20method%20returns%20a%20Promise%2C%20you%20can%20simply%20mark%20the%20function%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Easync%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ekeyword%20and%20add%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eawait%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eprefix%20before%20calling%20the%20asynchronous%20function%2C%20like%20in%20the%20following%20sample%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20getData%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Easync%3C%2FSPAN%3E%20()%20%3D%26gt%3B%20%7B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20result%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20fetch%20(%3CSPAN%20class%3D%22hljs-string%22%3E'this-is-a-url'%3C%2FSPAN%3E)%3B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2Fdo%20something%20with%20the%20result%3C%2FSPAN%3E%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ELet's%20start%20to%20see%20how%20we%20can%20build%20our%20module%20so%20that%20it%20can%20return%20a%20promise.%20It's%20easy%2C%20thanks%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EIReactPromise%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Einterface%20included%20in%20the%20React%20Native%20implementation%20for%20Windows.%20Let's%20see%20some%20code%20first%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eusing%3C%2FSPAN%3E%20Microsoft.ReactNative.Managed%3B%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Eusing%3C%2FSPAN%3E%20System%3B%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Eusing%3C%2FSPAN%3E%20Windows.Devices.Geolocation%3B%0A%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Enamespace%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGeolocationModule%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%5B%3CSPAN%20class%3D%22hljs-meta%22%3EReactModule%3C%2FSPAN%3E%5D%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eclass%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGeolocationModule%3C%2FSPAN%3E%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%5B%3CSPAN%20class%3D%22hljs-meta%22%3EReactMethod(%3CSPAN%20class%3D%22hljs-meta-string%22%3E%22getCoordinatesWithPromise%22%3C%2FSPAN%3E)%3C%2FSPAN%3E%5D%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Easync%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGetCoordinatesWithPromise%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3EIReactPromise%26lt%3B%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%26gt%3B%20promise%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Etry%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Geolocator%20geolocator%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Geolocator()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20position%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20geolocator.GetGeopositionAsync()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20result%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%24%22Latitude%3A%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7Bposition.Coordinate.Point.Position.Latitude%7D%3C%2FSPAN%3E%20-%20Longitude%3A%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7Bposition.Coordinate.Point.Position.Longitude%7D%3C%2FSPAN%3E%22%3C%2FSPAN%3E%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20promise.Resolve(result)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ecatch%3C%2FSPAN%3E%20(Exception%20e)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20promise.Reject(%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20ReactError%20%7B%20Exception%20%3D%20e%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EAs%20first%20step%2C%20we%20need%20to%20declare%20the%20method%20as%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Easync%20void%3C%2FCODE%3E%2C%20in%20order%20to%20be%20compliant%20with%20the%20React%20Native%20requirements.%20To%20handle%20the%20asynchronous%20nature%20of%20the%20method%2C%20the%20key%20component%20is%20the%20requested%20parameter%2C%20which%20type%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EIReactPromise%3CT%3E%3C%2FT%3E%3C%2FCODE%3E%2C%20where%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ET%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eis%20the%20type%20of%20the%20value%20we%20want%20to%20return.%20In%20my%20scenario%20I%20want%20to%20return%20a%20string%20with%20the%20full%20coordinates%2C%20so%20I'm%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EIReactPromise%3CSTRING%3E%3C%2FSTRING%3E%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Etype.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInside%20the%20method%20we%20can%20start%20writing%20our%20code%20like%20we%20would%20do%20in%20a%20traditional%20Windows%20application.%20Since%20we%20have%20marked%20the%20method%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Easync%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ekeyword%2C%20we%20can%20just%20call%20any%20asynchronous%20API%20(like%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGetGeopositionAsync()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eone%20exposed%20by%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGeolocator%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eclass)%20by%20adding%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eawait%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eprefix.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20we%20have%20completed%20the%20work%20and%20we%20have%20obtained%20the%20result%20we%20want%20to%20return%2C%20we%20need%20to%20pass%20it%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EResolve()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20exposed%20by%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EIReactPromise%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eparameter.%20The%20method%20expects%20a%20value%20which%20type%20is%20equal%20to%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ET%3C%2FCODE%3E.%20In%20my%20case%2C%20for%20example%2C%20we%20would%20get%20an%20error%20if%20we%20try%20to%20pass%20anything%20but%20a%20string.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20case%20something%20goes%20wrong%2C%20instead%2C%20we%20can%20use%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EReject()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20to%20surface%20the%20error%20to%20the%20React%20Native%20application%2C%20by%20creating%20a%20new%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EReactError%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject.%20You%20can%20customize%20it%20with%20different%20parameters%2C%20like%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ECode%3C%2FCODE%3E%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EMessage%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EUserInfo%3C%2FCODE%3E.%20In%20my%20case%2C%20I%20just%20want%20to%20raise%20the%20whole%20exception%2C%20so%20I%20simply%20set%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EException%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty%20exposed%20by%20my%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Etry%20%2F%20catch%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eblock.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat's%20it!%20Now%20we%20can%20easily%20define%20a%20function%20in%20our%20React%20Native%20code%20that%2C%20by%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ENativeModules%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EAPI%20and%20a%20Promise%2C%20can%20invoke%20the%20C%23%20method%20we%20have%20just%20created%2C%20as%20in%20the%20following%20sample%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20getCoordinatesWithPromise%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Easync%3C%2FSPAN%3E%20()%20%3D%26gt%3B%20%7B%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20coordinates%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20NativeModules.GeolocationModule.getCoordinatesWithPromise()%3B%0A%20%20%3CSPAN%20class%3D%22hljs-built_in%22%3Econsole%3C%2FSPAN%3E.log(coordinates)%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ESince%20we're%20using%20a%20Promise%2C%20the%20syntax%20is%20the%20same%20we%20have%20seen%20in%20the%20previous%20sample.%20We%20mark%20the%20function%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Easync%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ekeyword%20and%20we%20call%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EgetCoordinatesWithPromise()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eawait%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eprefix.%20Thanks%20to%20the%20Promise%20we%20can%20be%20sure%20that%2C%20when%20we%20invoke%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Econsole.log()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction%2C%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Ecoordinates%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty%20has%20been%20properly%20set.%20The%20native%20module%20is%20exposed%20through%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ENativeModules%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EAPIs%20and%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGeolocationModule%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject%2C%20which%20is%20the%20name%20of%20the%20C%23%20class%20we%20have%20created%20(if%20you%20remember%2C%20we%20have%20marked%20it%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3E%5BReactModule%5D%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eattribute).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22use-callbacks%22%20id%3D%22toc-hId--636078994%22%20id%3D%22toc-hId--636078994%22%3EUse%20callbacks%3C%2FH3%3E%0A%3CP%3EI%20really%20much%20prefer%20the%20syntax%20offered%20by%20Promises%20but%20if%2C%20by%20any%20chance%2C%20you%20prefer%20to%20use%20callbacks%2C%20we%20got%20you%20covered!%20You%20can%20use%2C%20in%20fact%2C%20a%20slightly%20different%20syntax%2C%20based%20on%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EAction%3CT%3E%3C%2FT%3E%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject%2C%20to%20expose%20your%20native%20C%23%20method%20as%20a%20callback.%20Let's%20see%20the%20implementation%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-121%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eusing%3C%2FSPAN%3E%20Microsoft.ReactNative.Managed%3B%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Eusing%3C%2FSPAN%3E%20System%3B%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Eusing%3C%2FSPAN%3E%20Windows.Devices.Geolocation%3B%0A%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Enamespace%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGeolocationModule%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%5B%3CSPAN%20class%3D%22hljs-meta%22%3EReactModule%3C%2FSPAN%3E%5D%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eclass%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGeolocationModule%3C%2FSPAN%3E%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%5B%3CSPAN%20class%3D%22hljs-meta%22%3EReactMethod(%3CSPAN%20class%3D%22hljs-meta-string%22%3E%22getCoordinatesWithCallback%22%3C%2FSPAN%3E)%3C%2FSPAN%3E%5D%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Easync%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGetCoordinatesWithCallback%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3EAction%26lt%3B%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%26gt%3B%20resolve%2C%20Action%26lt%3B%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%26gt%3B%20reject%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Etry%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Geolocator%20geolocator%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Geolocator()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20position%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20geolocator.GetGeopositionAsync()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20result%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%24%22Latitude%3A%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7Bposition.Coordinate.Point.Position.Latitude%7D%3C%2FSPAN%3E%20-%20Longitude%3A%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7Bposition.Coordinate.Point.Position.Longitude%7D%3C%2FSPAN%3E%22%3C%2FSPAN%3E%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20resolve(result)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ecatch%3C%2FSPAN%3E%20(Exception%20e)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reject(e.Message)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIn%20this%20case%2C%20as%20parameters%20of%20the%20method%2C%20we%20are%20passing%20two%20objects%20which%20type%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EAction%3CT%3E%3C%2FT%3E%3C%2FCODE%3E.%20The%20first%20one%20will%20be%20used%20when%20the%20method%20completes%20successfully%2C%20the%20second%20one%20when%20instead%20something%20goes%20wrong.%20In%20this%20case%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ET%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eis%20a%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Estring%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ein%20both%20cases.%20In%20case%20of%20success%2C%20we%20want%20to%20return%20the%20usual%20string%20with%20the%20full%20coordinates%3B%20in%20case%20of%20failure%2C%20we%20want%20to%20return%20the%20message%20of%20the%20exception.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20rest%20of%20the%20code%20is%20similar%20to%20the%20one%20we%20have%20written%20before.%20The%20only%20difference%20is%20that%2C%20when%20we%20have%20achieved%20our%20result%2C%20we%20just%20pass%20it%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eresolve()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction%3B%20when%20something%20goes%20wrong%2C%20instead%2C%20we%20call%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Ereject()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction.%20The%20main%20difference%20compared%20to%20the%20previous%20approach%20is%20that%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EAction%3CT%3E%3C%2FT%3E%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eisn't%20a%20structured%20object%20like%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EIReactNative%3CT%3E%3C%2FT%3E%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Einterface.%20As%20such%2C%20it%20doesn't%20support%20to%20pass%20the%20full%20exception%20but%2C%20in%20this%20case%2C%20we%20choose%20to%20pass%20only%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EMessage%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty%20of%20the%20exception.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat's%20if%20from%20the%20C%23%20side.%20Let's%20move%20to%20the%20JavaScript%20one.%20In%20this%20case%2C%20being%20a%20callback%2C%20we%20can't%20use%20anymore%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Easync%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eawait%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ekeywords%2C%20but%20we%20need%20to%20pass%20to%20the%20method%20two%20functions%3A%20one%20will%20be%20called%20when%20the%20operation%20is%20successful%2C%20one%20when%20we%20get%20an%20error.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-158%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20getCoordinatesWithCallback%20%3D%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-params%22%3E()%3C%2FSPAN%3E%20%3D%26gt%3B%3C%2FSPAN%3E%20%7B%0A%20%20NativeModules.GeolocationModule.getCoordinatesWithCallback(%20%3CSPAN%20class%3D%22hljs-function%22%3E(%3CSPAN%20class%3D%22hljs-params%22%3Eresult%3C%2FSPAN%3E)%20%3D%26gt%3B%3C%2FSPAN%3E%20%7B%20%3CSPAN%20class%3D%22hljs-built_in%22%3Econsole%3C%2FSPAN%3E.log(result)%3B%20%7D%2C%20(error)%20%3D%26gt%3B%20%3CSPAN%20class%3D%22hljs-built_in%22%3Econsole%3C%2FSPAN%3E.log(error))%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22passing-parameters%22%20id%3D%22toc-hId-1851433839%22%20id%3D%22toc-hId-1851433839%22%3EPassing%20parameters%3C%2FH3%3E%0A%3CP%3EIn%20both%20scenarios%2C%20if%20we%20need%20to%20pass%20any%20parameter%20from%20the%20JavaScript%20code%20to%20the%20C%23%20one%20we%20can%20just%20add%20them%20before%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EIReactPromise%3CT%3E%3C%2FT%3E%3C%2FCODE%3E%3CSPAN%3E%20or%20the%26nbsp%3B%3CCODE%3EAction%3CT%3E%3C%2FT%3E%3C%2FCODE%3E%26nbsp%3Bones%3C%2FSPAN%3E.%20For%20example%2C%20let's%20say%20we%20want%20to%20set%20the%20desired%20accuracy%20of%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGeolocator%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject%20with%20a%20value%20in%20meters%2C%20passed%20from%20the%20React%20Native%20app.%20We%20can%20just%20define%20the%20method%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-167%22%20class%3D%22language-csharp%20hljs%22%3E%5B%3CSPAN%20class%3D%22hljs-meta%22%3EReactMethod(%3CSPAN%20class%3D%22hljs-meta-string%22%3E%22getCoordinatesWithPromise%22%3C%2FSPAN%3E)%3C%2FSPAN%3E%5D%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Easync%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EGetCoordinatesWithPromise%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Euint%3C%2FSPAN%3E%20meters%2C%20IReactPromise%20%26lt%3B%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%26gt%3B%20promise%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Etry%3C%2FSPAN%3E%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Geolocator%20geolocator%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Geolocator()%3B%0A%20%20%20%20%20%20%20%20geolocator.DesiredAccuracyInMeters%20%3D%20meters%3B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20position%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20geolocator.GetGeopositionAsync()%3B%0A%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20result%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%24%22Latitude%3A%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7Bposition.Coordinate.Point.Position.Latitude%7D%3C%2FSPAN%3E%20-%20Longitude%3A%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7Bposition.Coordinate.Point.Position.Longitude%7D%3C%2FSPAN%3E%22%3C%2FSPAN%3E%3B%0A%0A%20%20%20%20%20%20%20%20promise.Resolve(result)%3B%0A%20%20%20%20%7D%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ecatch%3C%2FSPAN%3E%20(Exception%20e)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20promise.Reject(%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20ReactError%20%7B%20Exception%20%3D%20e%20%7D)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThen%2C%20from%20JavaScript%2C%20we%20just%20need%20to%20pass%20the%20value%20of%20the%20parameter%20when%20we%20invoke%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EgetCoordinatesWithPromise()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-191%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20getCoordinatesWithPromise%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Easync%3C%2FSPAN%3E%20()%20%3D%26gt%3B%20%7B%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20coordinates%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20NativeModules.GeolocationModule.getCoordinatesWithPromise(%3CSPAN%20class%3D%22hljs-number%22%3E15%3C%2FSPAN%3E)%3B%0A%20%20%3CSPAN%20class%3D%22hljs-built_in%22%3Econsole%3C%2FSPAN%3E.log(coordinates)%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EOf%20course%2C%20you%20can%20use%20the%20same%20pattern%20if%20you%20prefer%20to%20leverage%20the%20callback%20approach.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22wrapping-up%22%20id%3D%22toc-hId-43979376%22%20id%3D%22toc-hId-43979376%22%3EWrapping%20up%3C%2FH3%3E%0A%3CP%3EOn%20this%20blog%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-dev-appconsult%2Fbuilding-a-react-native-module-for-windows%2Fba-p%2F1067893%22%20target%3D%22_blank%22%3Ewe%20have%20already%20learned%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ehow%20to%20build%20native%20C%23%20modules%20for%20React%20Native%20so%20that%2C%20as%20a%20developer%2C%20you%20can%20access%20native%20Windows%20APIs%20from%20JavaScript.%20However%2C%20in%20that%20sample%20we%20leveraged%20only%20synchronous%20APIs.%20The%20reality%2C%20however%2C%20is%20that%20most%20of%20the%20Windows%2010%20APIs%20are%20implemented%20with%20the%20asynchronous%20pattern.%20Unfortunately%2C%20as%20soon%20as%20you%20start%20using%20them%2C%20you%20will%20face%20errors%20and%20crashes%2C%20since%20React%20Native%20isn't%20able%20to%20properly%20understand%20the%20traditional%20async%20%2F%20await%20pattern%2C%20based%20on%20the%20Task%20object.%20Thanks%20to%20the%20React%20Native%20team%20I've%20been%20pointed%20into%20the%20right%20direction%2C%20by%20leveraging%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EIReactNative%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Einterface%20(in%20case%20you%20want%20to%20enable%20asynchronous%20APIs%20with%20promises)%20or%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EAction%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject%20(in%20case%20you%20want%20to%20leverage%20the%20more%20traditional%20callback%20approach).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERegardless%20of%20the%20approach%20you%20prefer%2C%20you'll%20be%20able%20to%20achieve%20your%20goal%20of%20leveraging%20asynchronous%20Windows%2010%20APIs%20from%20your%20React%20Native%20application%20running%20on%20Windows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22FinalApp.png%22%20style%3D%22width%3A%20846px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F197301i6D55AC3AC86BD3EC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22FinalApp.png%22%20alt%3D%22FinalApp.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Version history
Last update:
‎Jun 09 2020 09:37 AM
Updated by:
www.000webhost.com