-1

I'm accessing google API for converting coordinates into detailed objects using node-geocoder library from npmjs. Everything went well and I'm getting the expected object from geocoder API. The problem started the moment when I thought of using the data outside the promise function. I want to use the values outside the promise/async-await function. Below is the code I've tried, Pls take a look and help me. TIA...

function goecoderPromiseFunction() {

    return new Promise(function (resolve, reject) {

        geocoder.reverse({ lat: 45.767, lon: 4.833 })

            .then(data => {

                cityName = data[0].city;

                resolve(cityName);

            })
            .catch(err => {

                console.log(err);

            });
    });

}

async function app() {

    var a = await goecoderPromiseFunction();
    return a;

}

var a = app();

console.log("a->", a);

I expect the variable "a" should print the city name "Lyon", but it prints

a-> Promise { < pending > }

Vivek K
  • 11
  • 4
  • Possible duplicate of [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) – ponury-kostek May 30 '19 at 11:47
  • I've read those answers and my question is may be similar but not same. My exact question is how to use the consumed promise values outside the function and anywhere in the code. – Vivek K May 30 '19 at 11:59

3 Answers3

2

The promise returned by the app function is never consumed, that is why it remains in a pending state.

Call then on the app function to get the result :

app().then(a => console.log("a->", a));

You can also use async/await :

 (async function() {
   var a = await app();
   console.log("a->", a);
 })();
Murat
  • 1,135
  • 8
  • 12
  • Hi MuratK, thanks for your answer. (async function() { var a = await app(); console.log("a->", a); })(); While applyig the above code, It prints the expected output but I want use that "a" variable outside the function like, (async function() { var a = await app(); console.log("a->", a); })(); console.log("a->", a); While printing outside it doesn't work! – Vivek K May 30 '19 at 11:41
0

It prints because console.log("a->", a); runs while promise haven't returned answer for a variable "a" Note: you haven't used reject function, if there is an error you wont notice and may be that error was the required answer to be carried by variable "a" that's why it still pending i.e still waiting. For more idea try to use reject function inside the catch block example reject(err) instead of console it out as you've done

ndotie
  • 1,830
  • 17
  • 18
0

An asynchronous function actually returns a promise that 'resolves' to the function's return value. You are therefore assigning a promise to the value of a. If you are in the global scope, you obviously cannot use async/await so you need to use either a self-executing async function or you need to run

a.then(data => console.log('a->', data));

to get what you are looking for.

Find out more about async functions here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

J. Stott
  • 194
  • 1
  • 16