A
A
Adel1ne2015-10-28 14:06:37
JavaScript
Adel1ne, 2015-10-28 14:06:37

How to get the result of a javascript function?

Hello!
I'm dumb and can't figure it out.
Let's say I have a function (from Open Api VK):

VK.Auth.getLoginStatus(function(response) {
        if (response.session) {
            /* Авторизованный в Open API пользователь */
            
        } else {
            /* Неавторизованный в Open API пользователь */

        }
    });
.
As you can see, inside the function, you can write some actions, depending on the result (response).
But the problem is that I do not need to write these actions inside the function. You just need to know how the function worked and then, with certain actions (for example, when clicking on a button), execute some code in one way or another.
Since the result of the function is not a specific return, assigning the function to a variable will not work.
Advise some simple way to get the result of the function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Ineshin, 2015-10-28
@Adel1ne

If the function is executed synchronously, then you can simply insert return true;or inside the conditions return false;
. For example:

function calc (a, b) {
    return a + b;
}

var result = calc(2, 2);
console.log(result) // 4

If the function is asynchronous, as your example is most likely. Then you can’t directly return the value of the function, you can only write values ​​to some variables:
var auth;

VK.Auth.getLoginStatus(function(response) {
        if (response.session) {
            /* Авторизованный в Open API пользователь */
            auth = true;
            next();
        } else {
            /* Неавторизованный в Open API пользователь */
            auth = false;
            next();
        }
    });

// продолжаем работу
function next () {
    //...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question