Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why the execute function on APIOperationBase class does not return the response? #69

Open
comeacross1989 opened this issue Nov 1, 2019 · 3 comments

Comments

@comeacross1989
Copy link

Hi, I'm using a firebase callable function and would like to send the repsponse back to the client once the transaction is completed - instead of sending another post to a different URL. The only problem is that the APIOperationBase.execute does not return the response.
Is there a reason for this?

It would be much more efficient if I could do this...
` try {

    const THE_RESPONSE_FOR_CLIENT = ctrl.execute(function(){

        var apiResponse = ctrl.getResponse();
        var response = new ApiContracts.CreateTransactionResponse(apiResponse);

        //pretty print response
        console.log(JSON.stringify(response, null, 2));

        if(response != null){
            if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK){
                if(response.getTransactionResponse().getMessages() != null){
                    console.log('Successfully created transaction with Transaction ID: ' + response.getTransactionResponse().getTransId());
                    console.log('Response Code: ' + response.getTransactionResponse().getResponseCode());
                    console.log('Message Code: ' + response.getTransactionResponse().getMessages().getMessage()[0].getCode());
                    console.log('Description: ' + response.getTransactionResponse().getMessages().getMessage()[0].getDescription());
                    
                   
                }
                else {
                    console.log('Failed Transaction.');
                    if(response.getTransactionResponse().getErrors() != null){
                        console.log('Error Code: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorCode());
                        console.log('Error message: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorText());
                        
                      }
                }
            }
            else {
                console.log('Failed Transaction. ');
                if(response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null){
                
                    console.log('Error Code: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorCode());
                    console.log('Error message: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorText());
                    
                  }
                else {
                    console.log('Error Code: ' + response.getMessages().getMessage()[0].getCode());
                    console.log('Error message: ' + response.getMessages().getMessage()[0].getText());
                   
                  }
            }
        }
        else {
            console.log('Null Response.');  
        }
        callback(response,finalMessage);
        console.log('response,finalMessage');
        console.log(ctrl._response );
    });


    return THE_RESPONSE_FOR_CLIENT;
} catch (error) {
    throw new functions.https.HttpsError('unknown', error);
}`
@comeacross1989
Copy link
Author

I guess a better question would be, why is it an async callback function? I want to send the response back to the user!

@Marcusg62
Copy link

Marcusg62 commented Nov 2, 2019

Look at this stack overflow question. By definition all callback functions are async so you would have to force your code to wait for the promise to resolve to send it back to the user.

The problem is, if you're using an environment like gcp or firebase, you won't be able to change the auothorizenet npm package in the cloud to force the await for promise to resolve. Not sure what to tell you there. Fork off of this package? I'm curious what the contributors think.
@sinayak @maria-simon @lilithmm

@ahmed-masud
Copy link

Your code attempt would cause a LOT of issues because you are assuming not one but two network connections are going to remain stable during the entire transaction. There is a reason why it's built async and with promises. Also your code looks like java instead of taking advantage of Promises ... You really need to rethink your design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants