0

I'm making a API call/fetch(), to check a user's logged in status and I need to return true or false from the fetch. (Don't ask why, I just need to do this specifically for a project at work, so must be done this way).

I have some code below:

  • which returns a fetch
  • And returns the response successfully, outside the main function checkLoggedInStatus().
  • However, if the logged in status === NotloggedIn, I want to return true.

In this example below, my code returns NotloggedIn. But does not return true.

How can I do this?

function checkLoggedInStatus() {
    let data;

    return fetch("www.MYAPI.com")
        .then((response) => response.text())
        .then((result) => {
            data = JSON.parse(result);
            return data;
        })
        .catch((error) => console.log("error", error));
};

checkLoggedInStatus().then(data => {
    console.log("data.Status", data);
    if (data === "NotLoggedIn") {
        return true;
    } else {
        return false
    };
})
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Reena Verma
  • 1,617
  • 2
  • 20
  • 47
  • 2
    With your code above, the promise created by calling `checkLoggedInStatus().then` will be fulfilled with `true` or `false`. You have to use the result of that promise. – T.J. Crowder May 09 '21 at 10:44
  • 2
    FWIW, a couple of notes on that code: 1. Your code is falling prey to the `fetch` API footgun I describe [here](http://blog.niftysnippets.org/2018/06/common-fetch-errors.html): You need to check `response.ok` to see if the HTTP operation succeeded. 2. The `Response` object has a `json` method that both reads the response and parses it from JSON, no need to use `text` and then `JSON.parse`. 3. There's no reason for the `if`/`else` in the code at that end, just do `return data === "NotLoggedIn";` It's *already* a boolean. 4. Declare `data` only inside the function that uses it. – T.J. Crowder May 09 '21 at 10:47
  • Thanks @T.J.Crowder - why did you close the question though? Appreciate you sent a link, but I would like to have seen other people's responses and some suggestions to my code/amendments.... – Reena Verma May 09 '21 at 10:59
  • 2
    Adding to the issues mentioned in the above comment, callback function of `catch()` method in the `checkLoggedInStatus` implicitly returns undefined, this means that it will implicitly convert promise rejection into promise fulfilment, leading to the invocation of the fulfilment callback in the calling code. – Yousaf May 09 '21 at 10:59
  • How can I rewrite this better? Sorry a lot of comments and higly confused lol – Reena Verma May 09 '21 at 11:00
  • Because [that's how SO works](https://stackoverflow.com/help/duplicates): This question is answered by the answers there, so rather than reposting the same thing over and over on every question about it, we direct the person with a subsequent question to the previous answers. – T.J. Crowder May 09 '21 at 11:04
  • @T.J.Crowder - but i need to specifically `return` a value, ( There's no reason for the if/else in the code at that end, just do return data === "NotLoggedIn";) – Reena Verma May 09 '21 at 11:04
  • @ReenaVerma - `if (data === "NotLoggedIn") { return true; } else { return false; }` and `return data === "NotLoggedIn";` do **exactly** the same thing: They return `true` if `data` is `"NotLoggedIn"` and `false` if it isn't. – T.J. Crowder May 09 '21 at 11:05
  • i get that but its confusing, as I'm getting links and comments and then saying I don't need to return anything. I've taken time to figure out a solution and post a decent question. And then gets a mish mashed response. – Reena Verma May 09 '21 at 11:05
  • @ReenaVerma - I don't see anything from anyone saying you don't have to return anything. – T.J. Crowder May 09 '21 at 11:06
  • 2
    [Here's how you can refactor your code](https://pastebin.com/7eRsz8nW) – Yousaf May 09 '21 at 11:16
  • 1
    @T.J.Crowder - Apologies TJ, it took a while for me to really understand your comments. You're right, returning the code this way works. – Reena Verma May 09 '21 at 11:17
  • 1
    @ReenaVerma - That's great! I was just going to send you [this](https://pastebin.com/0pcntKYZ) because code is often clearer than a description of it. :-) I see Yousaf did the same sort of thing. Happy coding! :-) – T.J. Crowder May 09 '21 at 11:18
  • Thank you so much @Yousaf!! Really appreciate it! I'll take a look at this now to understand it!! – Reena Verma May 09 '21 at 11:18
  • Thank you so much guys, I really appreciate it! I don't work with any developers at work, so SO is always a last resort. And having some examples to work through and understand is so helpful. Really appreciate it guys. I'll start breaking this down now :) – Reena Verma May 09 '21 at 11:19
  • This works really well guys, thank you so much. Originally, i was actually checking response.ok and then completely went down a rabbit hole. – Reena Verma May 09 '21 at 11:30
  • Yousef, I really like how I can work with the true/false return, which is kinda what I needed (// isLoggedIn will be either true or false). Thank you all. – Reena Verma May 09 '21 at 11:31

0 Answers0