I have to get public ip addresses with a module called public-ip
.
The doc says that for this to work as intended, it need to be asynchronous and work with promises.
My problem is that I need to extract the value after the asynchronous method is fulfilled and use it into a synchronous function.
Let consider this code :
import public_ip from 'public-ip'
function test() {
public_ip.v4()
.then((data) => {
return data ;
})
}
const ip = test() ;
console.log('Your ip is : ' + ip ) ;
and it returns:
Your ip is : undefined
Process finished with exit code 0
instead of returning my ip is xxx.xxx.xxx.xxx
but when I console out , with this minor change :
import public_ip from 'public-ip'
function test() {
public_ip.v4()
.then((data) => {
console.log (data) ;
})
}
test() ;
it returns my ip. Now I understand that with promise the data processing have to happen within the async method like the following :
.then((data) => {
//... data processing here ...
})
but is there a way to extra the data after the event loop closes and process it in a synchronous way ?
Thanks for helping out.