Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

return values for asynchronous javascript calls #30

Closed
KarthickR opened this Issue · 3 comments

2 participants

@KarthickR

Hi marcus,

i have a javascript method like this

         phoneBridge.isAvailable=function isAvailable(featurename){

               bridge.callhandler('registerIsAvailable',featurename,function(response){
                           return response;
        })
     }

Since the call to this method is asynchronous, i could not returnn the values. Javascript is not waiting untill the method is executed.Could you please suggest me a way to return the values.Please help me to solve this issue.

@marcuswestin

Hi @KarthickR,

This is not an issue with WVJB, but I understand your problem.

In short, you shouldn't rely on returning values in asynchronous functions but rather call a function when the response is ready which then proceeds to use the response.

Below is an example that should do something similar to what you want. I haven't tested the code, so probably just read it to understand the issue.

You'd probably be best off studying asynchronous programming a bit in general though! Google around a bit on it and on functional programming and you'll probably be able to get more comfortable with this kind of stuff.

Best of luck!

Cheers,
Marcus

// Objc

[_bridge registerHandler:@"featureIsAvailable" handler:^(id data, WVJBResponseCallback responseCallback) {
    if ([[data objectForKey:@"featureName"] isEqualToString:@"chat"]) {
        responseCallback([NSDictionary dictionaryWithObject:@"yes" forKey:@"isAvailable"]);
    } else {
        responseCallback([NSDictionary dictionary]);
    }
}];

//...

// Javascript

phoneBridge.isAvailable = function isAvailable(featureName, callback) {
    bridge.callhandler('featureIsAvailable', { featureName:featureName }, function(response){
        callback(!!response.isAvailable)
    })
}

phoneBridge.isAvailable('chat', function(isAvailable) {
    if (isAvailable) {
        alert("Chat is available!")
    } else {
        alert("Chat is disabled :/")
    }
})
@KarthickR

Hi marcus,

Thank you very much for your answer.Actually the problem is the javascript code required for the project is already written from the javascript no call back is passed as an argument, we are not suppose to modify anything in the js code. Below is the method what we are using. This method has calls to the functions in iOS as well as Android. We are not getting any issues in Android. Is there any other similar way to modify our iOS function call to achieve this.Please help us.

                                          VestaPhoneBridge.IsAvailable = function(featureName)
                                          {

                                                  if(isAndroid()){
                                                       if(typeof VestaJavascriptInterface !== 'undefined')
                                                       {

                                                                return VestaJavascriptInterface.isAvailable(featureName);

                                                         }
                                                        return false;
                                          } 
                                          else{
                                               bridge.callHandler('initiateIsAvailableFunction',featureName,function(response) {
                                                                     return response;
                                              })
                                         }

                                     };
@marcuswestin
@martin-magakian martin-magakian referenced this issue in snakajima/neunode
Open

neunode.a source #2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.