Why is he not waiting for my answer?
It is simply because you are returning a promise. Node js is the only thread and is executed in a non-blocking way.
This means that the return response in your get function is executed before the response variable is resolved.
Try the following:
let request = require('request-promise')
function get(url) {
let _opt = {}
return request(url, _opt);
}
async function some () {
console.log(await get('http://www.httpbin.org/ip'));
}
some();
This example also returns a promise, but in this case, we expect to resolve the promise.
I hope for this help.
source
share