-2

I don't understand why it doesn't get in then(),

I am trying to check token exists or not

utils.js

export const isTokenExists = () => {
  return new Promise((resolve) =>
    setTimeout(() => {

      const token = localStorage.getItem("Token");

      if ((token === null) | (token === undefined)) {
        console.log("token null or undefined");           //printing in console log if token is null or undefined
        Promise.resolve(false);
      }
  
     Promise.resolve(true);
   }, 3000)
 );
};

export const tokenExist = async () => {
  const result =await isTokenExists()
  .then((resp) => {   //totally
    console.log('then called')
    console.log(resp);
    return resp;
  });
  console.log(result)
  return result;
};

then() is not printing anything in console log

I want to try some logic based on token exists or not, it should work async. in my case it always return true. I don't understand the logic why it returns always true

Signin.js

const tokenResult = tokenExist();
if(tokenResult){
  console.log('token result success')   //always token result success
}
else{
  console.log('token result failed')
}

updated

export const tokenExist = async () => {
  const result =await isTokenExists()
  
  console.log(result) //yes it print either true or false
  return result;
};

but in signin.js is still always true

Liam neesan
  • 2,282
  • 6
  • 33
  • 72
  • 1
    If you're in an `async` function you should just `await` and forget all this `then` nonsense. You only really need `then` outside of an `async` function, otherwise `await`. – tadman Aug 06 '23 at 20:15
  • Why are you async wrapping a non-async function like `getItem`? – tadman Aug 06 '23 at 20:16
  • 4
    Your promise never resolves. You need to use the passed in `resolve` function, not `Promise.resolve`. – deceze Aug 06 '23 at 20:19
  • 2
    `Promise.resolve(value)` creates a `new Promise` that has nothing to do with the one you return and want to `resolve`. – Thomas Aug 06 '23 at 20:22
  • 2
    And `|` is a [bitwise OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR) not a [logical OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR). That would be `||` – Thomas Aug 06 '23 at 20:25
  • @AndyRay i read it, for ES2017+ promise with async and await, no need of then() and catch() – Liam neesan Aug 06 '23 at 20:26
  • Also I updated with resolve(value) instead of to create a new promise.resolve(value) – Liam neesan Aug 06 '23 at 20:27
  • Still `tokenResult = true` and print `token result success` always. why it is? – Liam neesan Aug 06 '23 at 20:29
  • The duplicate question shows you the ES2017 syntax. This question is an exact duplicate. And you need to do `const tokenResult = await tokenExist();` as shown in the duplicate. But this question should be closed. – Andy Ray Aug 06 '23 at 20:30
  • @AndyRay sigin.js is react component, I cannot make as async for signin.js, I am trying to separate the code to check token, that's I have written in utils.js file instead of directly write in sigin.js – Liam neesan Aug 06 '23 at 20:35
  • Why can't you `console.log` success / failed before `resolve(true)` or `resolve(false)` ? – Yaroslavm Aug 06 '23 at 20:40
  • 1
    @Liamneesan: Is tokenResult *true* or merely *”truthy”*? The code doesn’t differentiate between the two. You didn’t await tokenExist() so you’re checking the Promise object, not the resolved result. – David Aug 06 '23 at 20:46
  • @Liamneesan please read the duplication question. Once you're in an async flow, everything that uses it has to be async. You should also research how to handle async / loading states in React, which is probably what you are really asking. – Andy Ray Aug 06 '23 at 20:56

1 Answers1

0

Inside promise in isTokenExists you should call resolve() and not Promise.resolve() as far as your in you current code promise is hung in pending status forever.

export const isTokenExists = () => {
  return new Promise((resolve) =>
    setTimeout(() => {

      const token = localStorage.getItem("Token");

      if ((token === null) || (token === undefined)) {
        console.log("token null or undefined");           
        resolve(false);
      }
  
     resolve(true);
   }, 3000)
 );
};
Yaroslavm
  • 1,762
  • 2
  • 7
  • 15