i was trying out promise code but it always returns me resolve even if the user does not exist in the database can anyone help me fix my code and the return statement in the return function the the second console log is only working.
here is my code
Api Call
const email = '[email protected]';
const request = require('request');
function IsUserExists(email, kc_accessToken) {
let url = `${path}/users?email=${email}`;
return new Promise(function (resolve, reject) {
request(
{
url: url,
headers: {
'content-type': 'application/json',
authorization: `Bearer ${kc_accessToken}`,
},
},
function (error, response, body) {
if (error) {
console.log('some error occured');
}
if (response.body.length > 0) {
console.log('User Exist');
return resolve();
}
console.log('Does not Exist');
return reject();
}
);
});
}
Function Call
http
.createServer(function Test() {
getAccessToken()
.then(function (response) {
kc_accessToken = response.data.access_token;
IsUserExists(email, kc_accessToken).then((resp) => {
if (resp) {
console.log('Do Not Create');
} else if (!resp) {
console.log('Creat a new User');
}
});
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
})
.listen(8081);
When Provided user email which exist ( [email protected] )
When Provided user email which does not exist( [email protected] )


I need to create a new answer for example to you question in comments.
Now, you go into the reject function so you need to handle this rejection in the outside.
You need add
.catchfunction afterIsUserExists.then().It will be
IsUserExists.then().catch()By the way, you could add parameter in rejection function like
reject(new Error("user not found)).Then in the outside, you can get this rejection message.