0

I'm trying to return the list created with the value received from API to where it was called. I'm using Promise to make sure the function returns the value after it gets the value from calling API. To better explain with the code, I have a function callAPI that calls getNamedEntity. Ultimately, I want callAPI to receive the result of request.post() in getNamedEntity. However, I get Promise object and undefined when I test with console.log("outside " + result) and console.log("inside doRequest " + result) inside each function respectively.

function callAPI(text) {
    const result = getNamedEntity(text);
    console.log("outside " + result);
}
async function getNamedEntity(text) {
  var requestJson = {
      ...
  };

  var options = {
      ...
  };

  function doRequest(options) {
    return new Promise(function (resolve, reject) {
      request.post(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
          const result = resolve(body);
          console.log("inside doRequest " + result);
        } else {
          reject(error);
        }
      });
    });
  }

  doRequest(options)
    .then((body) => {
      var entityList = [];
      const obj = JSON.parse(body);
      const words = obj.return_object.sentence[0].NE;
      for (var word of words) {
        entityList.push(word.text);
      }
      return entityList;
    })
    .catch((error) => {
      console.log(error);
    });
}
outside [object Promise]
inside doRequest undefined
epoch21
  • 1
  • 2
  • Well, one issue is that `async` returns a promise so you need to `await` the call to `getNamedEntity` in `callApi`, or use `then`. – Andy Apr 27 '22 at 04:21
  • @Andy I get syntax error `await is only valid in async function` when I change to `await getNamedEntity(text)` inside `callAPI` perhaps I'm missing something? – epoch21 Apr 27 '22 at 04:25
  • Change it to `return doRequest(options)...` and use `getNamedEntity(text).then(console.log)` – Phil Apr 27 '22 at 04:37

1 Answers1

0

console.log("outside " + result) returns a promise object cause you forgot to await the getNamedEntity(text); or, alternately, put the console.log inside a .then, like getNamedEntity(text).then(result => console.log(result));

console.log("inside doRequest " + result); returns undefined because the resolve function returns void, ie, it returns nothing. Change to console.log("inside doRequest " + body); or move the console.log into the .then((body) portion.

Ron Newcomb
  • 2,886
  • 21
  • 24
  • I get syntax error `await is only valid in async function` when I change to `await getNamedEntity(text)` inside `callAPI` perhaps I'm missing something? – epoch21 Apr 27 '22 at 04:31
  • correct, either that function also needs to be `async` or you use the `.then` method. Those new to promises say that async or promises "infect" their code. But basically where `await` sits is where the function ends and the rest of the function is basically a separate callback function – Ron Newcomb Apr 27 '22 at 04:36