Skip to content

Exporting async functions from Android #3436

@ide

Description

@ide

On iOS if the last two parameters of your RCT_EXPORT'd method are an RCTPromiseResolveBlock and an RCTPromiseRejectBlock, it will mark the exported method as an ES7 async function. When the JavaScript side of the bridge is set up, it knows it should define functions that return promises when it sees that some exported methods are marked as ES7 async functions.

The JS side is already done, so it's just some Java API design and code that needs to be written to get this on Android. I am not sure of the API right now. We could do a couple of things:

1/ Be like iOS and define two new types: ResolveCallback and RejectCallback. If we detect (via reflection) that the last two parameters of the exported method have these types, then we infer that it is an async function. Example syntax:

@ReactMethod
public void doSomethingAsync(String arg1, ResolveCallback resolve, RejectCallback reject) { }

2/ Add parameters to @ReactMethod. Same proposal as before but instead of using reflection, the programmer has to say something like this:

@ReactMethod(async = true)
public void doSomethingAsync(String arg1, ResolveCallback resolve, RejectCallback reject) { }

3/ Create a new annotation like @ReactAsyncMethod.

I prefer option 1 since it is consistent with RN iOS and because BaseJavaModule already uses reflection, but wanted to put a couple proposals out there. cc @mkonicek @kmagiera

Original iOS PR: #1232

cc @vjeux this is the last major piece of infra to making most of the codebase use async functions. The migration plan is pretty clear to me and can go into that w/more detail later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions