The documentation for writing Capacitor plugins mentions that for Android, if your plugin method does not return any data, then you can annotate your plugin method with “returnType=PluginMethod.RETURN_NONE”. Capacitor documentation link to this scenario is here: Capacitor - build cross platform apps with the web
To summarize, the Java code in Android to define the method looks like this:
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
public void method1(PluginCall call) {
android.util.Log.i(“method1”,“Inside method1 java code”);
return;
}
And back in TypeScript/JavaScript land, the code to define this same method looks like this:
export interface MyPlugin {
method1(): Promise<void>;
}
So based on the capacitor documentation, for a capacitor plugin method which does not return any data, that method returns a Promise which you should be able to “await” in TypeScript/JavaScript.
The problem is that the promise/await doesn’t work for the above sample code. For example, the following TypeScript/JavaScript code does not work as expected:
async myfunction() {
console.log(“About to call plugin method1”)
await MyPlugin.method1()
console.log(“Finished call to plugin method1”)
}
Given the above Android Java code and the above TypeScript/JavaScript, you would expected the log output to be:
About to call plugin method1
Inside method1 java code
Finished call to plugin method1
However, this is not what happens. Instead you get this:
About to call plugin method1
Finished call to plugin method1
Inside method1 java code
This indicates that the “await” in the TypeScript/JavaScript invocation of the plugin method is not really await-ing the completion of the promise returned by the plugin method. So it seems like this plugin method is not really returning a promise that can be await-ed.
You can solve the above problem by removing the “returnType=PluginMethod.RETURN_NONE” from the declaration of method1 in the Java code. If you remove that annotation and re-run the above example code, then the output is as expected:
About to call plugin method1
Inside method1 java code
Finished call to plugin method1
But this contradicts what the capacitor documentation says in the link at the beginning of my post here. The capacitor documentation claims that for PluginMethod.RETURN_NONE, you still get a JavaScript promise, which should be await-able.
So is the capacitor documentation incorrect, or is capacitor itself not working correctly for me?
1 post - 1 participant
Read full topic