1

I am trying to use fetch API to get my data from a url. I am able to fetch the data from url and perform operations with it. The issue i have is my return statement returns promise pending instead of returning true or false. I am able to use console.log to print result with no problem. Any help will be appreciated.

const fetch = require("node-fetch");

async function load(CLIENT_ID) {
  let response = await fetch('<url-hidden>');
  let data = await response.json();
  let resp_data = checkClientExist(data, CLIENT_ID)
  return resp_data
}

function checkClientExist(data, CLIENT_ID) {
  for (i = 0; i < data.length; i++) {
    if (data[i].CLIENT_ID === CLIENT_ID) {
      return true
    }
  }
  return false
}

console.log(load("fa000")) // Returns undefined want it to return either true or false(i.e resp_data from above)

load("fa000").then(response => {
  console.log(response)
}) // works fine
robbmj
  • 16,085
  • 8
  • 38
  • 63
Dev
  • 69
  • 2
  • 10
  • 2
    The example you have in the last line, the one that works, that's how you do it. Alternatively, if you're in the context of an `async` function, you can `await load("fa000")`. But asynchronous is just that... asynchronous. The `load` function doesn't return a boolean, it returns a `Promise`. – David Aug 06 '20 at 19:41

0 Answers0