React native android NativeModule promise does not resolve or reject

Viewed 2589

I built a react native NativeModule for Android which has a @ReactMethod i call from javascript.

The method launches an activity in which the user will do some stuff and then go back to the react activity. After inspecting the Activity result I decide to resolve or reject the promise.

But the promise.resolve(...) or promise.reject(...) have no effect in JS side. I never get the promised result. Maybe it has to do with some Thread problem? Or maybe the promise is getting resolved before the React activity is back to the foreground?

Why would the promise.resolve() not resolve the promise on the JS side?

This is the method I call from JS

 @ReactMethod
    public void login(final String appClientID, final String appSecretID, final String appName, final String returnURLScheme, Promise promiseCallback) {

        // Clear promise if there was a pending one
        if (promise != null) {
            promise.reject("Login error", "Pending promise found, so we reject it. Did you try to call login twice?");
            promise = null;
        }

        // Get foreground activity
        Activity mContext = getCurrentContext();
        // Configure settings
        OAuthLogin mOAuthLoginModule = OAuthLogin.getInstance();
        mOAuthLoginModule.init(mContext, appClientID, appSecretID, appName);
        // Start oauth login in a new activity
        mOAuthLoginModule.startOauthLoginActivity(mContext, mOAuthLoginHandler);
        // Store the promise to resolve later.
        promise = promiseCallback;
//  If i do here   promise.resolve('authorized');  the promise works!

    }

I am storing the promise object in the class to resolve or reject it later when the activity finishes. But it won't notify JS side.

This is the Activity callback which is executed when the user finishes log in.

public static void setUpCallback(final Context mContext) {
        mOAuthLoginHandler = new OAuthLoginHandler() {
            @Override
            public void run(final boolean success) {
                OAuthLogin mOAuthLoginModule = OAuthLogin.getInstance();
                if (success) {
                    String accessToken = mOAuthLoginModule.getAccessToken(mContext);
                    promise.resolve(accessToken); // This does not work. JS promise is never resolved and goes to limbo
                    promise = null;
                } else {
                    String errorCode = mOAuthLoginModule.getLastErrorCode(mContext).getCode();
                    String errorDesc = mOAuthLoginModule.getLastErrorDesc(mContext);
                    promise.reject("Oauth Login error code " + errorCode, errorDesc);  // This does not work. JS promise is never rejected and goes to limbo
                    promise = null;
            }
        }
      };
    }
2 Answers
Related