Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return a boolean after promise has been resolved/rejected?

I have a function that works absolutely fine. I just want to return true or false depending on the promise.

 //I want this function to return a simple true or false!!!
function isAppOnline() {
            var is_connected = connectivityMonitor.isInternetConnected();
            is_connected.then(function(result) {
                console.log('INTERNET_CHECK_API : app online');//works fine
                return true;//this is not being returned
            }, function(error) {
                console.log('INTERNET_CHECK_API : app offline');//works fine
                return false;//this is not being returned
            });
        }

But when I call this function,

is_online = isAppOnline();

is_online is always undefined . Why is the function not able to return a simple boolean value?

Update :

This is what I am trying to do : I simply want to open a popup which notifies the user that he is offline. I am calling the function isAppOnline periodically after 10secs. This function is already using a promise in my factories . I dont want to overcomplicate things but its important to me that this function returns a boolean so based on this,I can take my actions accordingly.

like image 302
HIRA THAKUR Avatar asked Oct 20 '15 07:10

HIRA THAKUR


People also ask

How do I return after Promise is resolved?

resolve() method in JS returns a Promise object that is resolved with a given value. Any of the three things can happened: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state.

How do I return a Boolean from Promise?

Answers. When your promise code calls resolve, it can pass the result for the promise, or a new promise that when resolved returns the value. So if to resolve, your code needs to maker another async call it can return a new promise. so in example 1, your resolve returns the value.

What does it mean to return a Boolean?

It's a boolean result of an evaluation - true or false . If it's true, do something. Using it with return returns that boolean result to the caller.


1 Answers

EDIT: For ES2017 If your one of the lucky souls who gets to use ES2017 then you can use the new await/async keywords. They are pretty brilliant and allow you to write async code that reads synchronous. (It is still promises under the hood, just unboxing of them).

function isOnline() {
    return Promise.resolve(true);
}

async function Main() {
    const online = await isOnline();

  console.log(online);
}

Main();

fidle


Because it is asynchronous. Your isAppOnline method returns before your promise has resolved.

I presume is making some form of AJAX call to check network connectivity so there it will have to wait for it to respond. JavaScript is single threaded, if that thread locked up waiting for that request to respond so it could be synchronous your whole JavaScript would pause until it returns. Not good.

So if you want the caller of isAppOnline to know the result you have to options. Either passing it a call back or return the promise (better option)

function isAppOnline(cb) {
    var is_connected = connectivityMonitor.isInternetConnected();
    is_connected.then(function(result) {
        console.log('INTERNET_CHECK_API : app online');//works fine
        cb(true);
    }, function(error) {
        console.log('INTERNET_CHECK_API : app offline');//works fine
        cb(false);
    });
}

//better option
function isAppOnline() {
    return connectivityMonitor.isInternetConnected().then(function(result) {
        console.log('INTERNET_CHECK_API : app online');//works fine
        return true;
    }, function(error) {
        console.log('INTERNET_CHECK_API : app offline');//works fine
        return false;
    });
}

//used as
isAppOnline().then(function (isOnline) {
    console.log('Is it online?', isOnline);
});
like image 146
ste2425 Avatar answered Sep 22 '22 11:09

ste2425