1

I am trying to call an Android native function "refreshPages" from JS function "refreshPagesWithIds" which passes an array of strings to native.

id: ["1","2"];
refreshPagesWithIds(id) {
  return Android.refreshPages(id);
}

The Android native function "refreshPages" internally calls a method "someFunction" which takes id array as argument and returns a Promise after refreshing the pages.

@JavascriptInterface
    public void refreshPages(String[] ids) {
        StorageModule.someFunction(ids, null);
    }

someFunction looks like:

@JvmStatic
    fun someFunction(pageIds: ReadableArray, promise: Promise? = null) {
        val storagePromise = StoragePromise(promise, storageModules.size)
        storageModules.forEach { module -> module.refreshPagesWithIds(pageIds, storagePromise) }
    }

How can I return a value when the Promise from refreshPages function(in native) gets settled to Javascript? I am unable to understand the implementation for public void refreshPages(String[] ids)

1 Answer 1

1

A java promise is different than a javascript promise, the only thing related between them is the name. You can not return a promise from java to javascript, even if syntax enables that you will be sending a serialised cloned object which has no relation to the one in java - the one in java might be completed, but you wont know about that in javascript.

What you could do is in javascript something like this

arePagesRefereshed: true
id: ["1","2"];
refreshPagesWithIds(id) {
  // this seems to be sitting in an object, so i am just assuming that this refers to the object, but you get the point
  this.arePagesRefereshed = false
  return Android.refreshPages(id);
},
markPagesAsRefereshed: () => {
  this.arePagesRefereshed = true

}

and then in Java

@JvmStatic
    fun someFunction(pageIds: ReadableArray, promise: Promise? = null) {
        val storagePromise = StoragePromise(promise, storageModules.size)
        storageModules.forEach { module -> module.refreshPagesWithIds(pageIds, storagePromise) }
    // after all promises are completed call markPagesAsRefereshed in javascript
    }
Sign up to request clarification or add additional context in comments.

2 Comments

When promise in Android gets resolved, how can I send value to JS?
@AashnaNarula Well call a javascript function with that value, for example call markPagesAsRefereshed(resolvedValue)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.