Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:15:05+00:00 2026-06-03T07:15:05+00:00

How can I return value from enclosing js function? I’ve tried next example, but

  • 0

How can I return value from enclosing js function?
I’ve tried next example, but it doesn’t work(it returns me:

function executeCommand(data, func){                
$.ajax({
type: 'POST',
url: SERVERPATH+'Commands/',
data: data,
success: func,
dataType: 'json'
});     
}

function executeServiceOperation(data){
var result = null;
executeCommand(data,
result = (function(data,status){        
      if( status=='success' ){                                  
        return data.result;     
      }
      else return null;          
    })(data,status)
);
 return result; 
}

result is null everytime. I think it happen because of status. How can I get status variable? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:15:06+00:00Added an answer on June 3, 2026 at 7:15 am

    Acording to your code, you must pass a function in the second parameter but you are returning an assignment (which is equivalent “true”). It will fail.

    instead, do something like this:

    executeCommand(data, function(data,status){ //function as second parameter       
        if( status=='success' ){                                  
           return data.result;     
        } else {
           return null;  
        }        
    });
    

    However, it’s still wrong. Anything involving AJAX is anynchronous. Thus you can’t “return” anything from it. What you should do is pass a callback when something “successful” happens.

    function executeServiceOperation(data, callbacks){
    
        //do some set-up stuff you want
    
        executeCommand(data,function(data,status){        
            if( status === 'success'){                              
                callbacks.success(data.result) //execute callback with result
            } else {
                callbacks.fail(null);     //execute fail callback, with null if necessary
            }          
        });
    );
    
    executeServiceOperation(data, {
        success :function(result){
            //do something with result
        },
        fail : function(){
            //do something as if it returned null
        }
    });
    

    this code above is written in the traditional callback fashion. you should take a look at Deferred Objects for “sugary” code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to return a value from (transport) to the calling function, but can't
How can I return a value from ShowStatus elapsed event? Here is my code.
XmlHttpRequest works through callbacks. So how can I return a value? I tried to
Can a function return more than one value directly (i.e., without returning in parameters
I can't understend why variable s_return dont work $('.codeinput').change(function() { var s_return=; var to_check=this.value
How can I get the return value of a process? Basically I'm ** ShellExecute
How can I retrieve the return value of a stored procedure using iBatis.NET? The
How can I do this in Moq? Foo bar = new Foo(); Fake(bar.PrivateGetter).Return('whatever value')
I can't get the ObjectId for a model to return back with a value
I am using Datetime.Minvalue which return 1.1.0001. This value can not be stored to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.