1

As I am just trying to get weather its found or not found as I have mostly the code which can explain the stuff I guess

and the function form the below is

exports.verifyGroupUser = function(name) {
  const url = 'api.com'
  fetch(url)
    .then(
      function(response) {
        if (response.status !== 200) {
          console.log('Looks like there was a problem. Status Code: ' +
            response.status);
          return;
        }

        //getting list of habbo in the specific group
        response.json().then(function(data) {
          var isHabbo = ((habbo) => {
            return habbo.name === name;
          })
          var checkbname = data.find(isHabbo);
          //checkbname.then(checkbname)
          //return checkbname;

          var checkx = ((checkbname) => {
            if (checkbname == undefined) {
              return "not found"
            } else {
              return "found"
            }
          })
          console.log(checkx(checkbname)) //gives the answer i need 
          return checkx(checkbname) //gives undefined
        });

      }
    )
    .catch(function(err) {
      console.log('Fetch Error :-S', err);
    });
}

calling from another file is like

const portalcheck = require('../controllers/portalcheck')
var ans = portalcheck.verifyGroupUser(req.body.username)
                  console.log(ans)
itsrankit
  • 99
  • 1
  • 7
  • can promise can do stuff, as the above code return the undefined at no time but the console.log take some second to print out the answer – itsrankit Jun 24 '20 at 18:44
  • How does `checkx` ever return anything other than `"found"`? You're passing `ckeckbname` in it and as per the `isHabbo` function, that's an *object* that has a `name` property. So, `ckeckbname` will never be equal to the string `"undefined"`, since it's either an object *or* an actual `undefined` variable. In neither case would it pass the equality check. – VLAZ Jun 24 '20 at 19:09
  • sorry VLAZ i messed with some code here but undefined is not string.. as i corrected the code still not working.. – itsrankit Jun 24 '20 at 19:14
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – VLAZ Jun 24 '20 at 19:21
  • OK, also - you aren't returning anything properly. You have a `return` statement but in the `then` that doesn't continue the promise chain. At the very least, you should do `return response.json().then(/ * ... */)` but it would be better if broke it up in discrete steps each in a `then`, so it is easier to read and understand. – VLAZ Jun 24 '20 at 19:23
  • @VLAZ, I don't think it's a dupe of that one, the `console.log(ans)` would have logged a pending promise if the asker had put the correct return statements. Since they're already using promises, I think they would have been able to deal with that. The problem is some misconception about where the value from `return` is getting sent, as addressed in my answer and your comment just now. – Klaycon Jun 24 '20 at 19:24
  • @Klaycon it's not returning the promise where the processing is being done. – VLAZ Jun 24 '20 at 19:25
  • @VLAZ I acknowledge this in my prior comment and answer. – Klaycon Jun 24 '20 at 19:25
  • @Klaycon Yes, and the dupe for handling async values goes in depth into how to set up a promise chain or use `async`/`await` properly – VLAZ Jun 24 '20 at 19:26
  • @VLAZ true. I think the asker should read it, but I'm not convinced it would address this particular misconception about what `return` does in a promise callback. – Klaycon Jun 24 '20 at 19:27

1 Answers1

2

return only returns the value to the containing function, not any outer one.

So when you do:

return checkx(checkbname) //gives undefined

that doesn't send the return three functions up to verifyGroupUser, it sends the return value only to this function:

response.json().then(function(data) { // <-- here

This is okay, because that means your .then() returns a Promise which will resolve with the "found" or "not found" you're looking for, but you're currently discarding that promise instead of using it. So, return the Promise:

return response.json().then(function(data) { // <-- here

which will bring that Promise out to your fetch(url).then(...), but you're discarding this promise too, so you'd want:

return fetch(url)
  .then(
    function(response) {

The end result of this will be that the function verifyGroupUser returns a Promise which will resolve with "found" or "not found". You need to deal with this Promise using .then() or await, because the function contains asynchronous calls, so you can never get it to synchronously return the "found" or "not found". It must be a Promise and you must deal with it appropriately in your usage.

const portalcheck = require('../controllers/portalcheck')
portalcheck.verifyGroupUser(req.body.username).then(function(ans) {
  console.log(ans)
})
Klaycon
  • 10,599
  • 18
  • 35