1

I would like to know if it is possible to check if a function call is returning to a variable definition, or to the global scope.

function getName() {
    if (!isVariableDefinition()) {
        console.log("John Doe");
    } else {
        return "John Doe";
}

var name = getName()
// name == "John Doe"

getName()
// Should print "John Doe" to the console

I have code that calls for a remote procedure (via AJAX) from a server. At the server, if the procedure is a variable definition, it's name should be saved for other uses, if not, it should just return the results.

var result = RPC('getName')
// The server should receive "result" and the call

RPC('getName')
// The server should receive only the call

Is that syntax possible?

1
  • My answer remains accurate. RPC cannot tell how its return value is used, the syntax is not possible. You're trying to use JavaScript in ways it cannot be used. Additionally, if RPC uses AJAX to fetch a value, the value will not even be available to return. You will have to either return a promise or pass a callback function into RPC. Commented Oct 1, 2016 at 4:19

1 Answer 1

4

No, it's not possible. A function has no idea what is being done with the value it returns.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there any alternative for that?
@user6907394 No, there isn't. You need to explain what you're trying to do. Without knowing what your actual problem is, we can't offer solutions.
@user6907394 I realize, but my answer remains the same. RPC cannot work that way. You need to either use two different functions (a "getter" and a "setter") or pass some additional flag to RPC which tells it what to do with the value it fetches.
Got it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.