Reputation: 953
export const checkLoggedIn = () => {
return new Promise((resolve, reject) => {
apiConfig.fetchApi('/users/is_valid', {}, 'get', {})
.then((resp)=> {
resolve(true);
})
.catch((exception)=> {
reject(false);
})
.done()
})
}
Above is my user token checking code.
But function checkLoggedIn()
is giving like the below image
But I want to get true or false only. Please if you find the issue in code, comment it out
Upvotes: 1
Views: 786
Reputation: 6184
Please check the definitions of then and catch and the general definition and usage of a Promise.
It seems that in your example apiConfig.fetchApi
returns a Promise (because you're using then()
which works on a Promise). Thus you don't have to create a new Promise.
Your code could look like this (I excluded the response parameter here because you don't use it in your code snippet):
export const checkLoggedIn = () => (
apiConfig.fetchApi('/users/is_valid', {}, 'get', {})
.then(() => true)
.catch(error => { console.log(error); return false; })
);
If apiConfig.fetchApi
does not return a Promise, you can create a Promise. But then you have to work with the return value of apiConfig.fetchApi
and resolve or reject depending on its value.
Upvotes: 3