0

I am running into a strange problem. I am using a module to look up the geo location from the IP address. The lookup method by default is sync.

I converted the method to async using bluebird, but its promise never gets resolved or rejected!

Here is the snippet:

var Promise = require('bluebird');
var geoip = Promise.promisifyAll(require('geoip-lite'));


geoip.lookupAsync('52.39.138.72').then((r) => {
    console.log(r);
}).catch((err) => {
    console.log(err);
})

console.log(geoip.lookup('52.39.138.72').country + '^^^^');

In the above snippet, the last console.log always gets printed but neither of the statement inside then or catch gets executed. What could be the reason for this?

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
  • Neither promises nor callbacks convert functions to async. – slebetman Mar 21 '17 at 05:27
  • See this: http://stackoverflow.com/questions/25500466/asynchrous-callback/25500753#25500753 and this: http://stackoverflow.com/questions/29883525/i-know-that-callback-function-runs-asynchronously-but-why/29885509#29885509 to understand what async really is – slebetman Mar 21 '17 at 05:29

1 Answers1

2

In the above snippet, the last console.log always gets printed but neither of the statement inside then or catch gets executed. What could be the reason for this?

The function you are trying to promisify does not follow the required asynchronous calling convention so promisifying it this way will not work.

For Bluebird's promisify to work properly, the function you promisify must follow the node.js async calling convention. That means the function must take a callback as its last argument and that callback must be called with two arguments err and result when the operation completes. If the function does not follow this convention, then promisifying it will not work.

And, there is really no reason to take a synchronous operation and promisify it either. Promisfying it won't suddenly make its functionality asynchronous.

So, your promise is never getting resolved or rejected because the underlying function doesn't use a callback that gets called with the right calling convention.

So, if geoip.lookup('52.39.138.72') is completely synchronous (as it appears to be) and gets called this way, then the underlying operation isn't asynchronous so there is no reason to even try to promisify it.

If you explain what problem you're actually trying to solve by promisifying it, we could likely offer another way (perhaps in a new question). One thing to keep in mind about stack overflow. If you describe your actual problem and show us the relevant code rather than asking about issues with one attempted solution, then we are much more likely to be able to help you and to offer you the best solution.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I am continuously reading a file, one line at a time. And I want to detect two conditions from the line parsed. Detecting IP location is one of the conditions. But I think I am unable to manage the promises. This is a small sample of containing two files `index.js` and `lib/parser.js` https://github.com/suhailgupta03/Near-Real-Time-Threat-Detection . If you look at `app.post('/upload'` you will get an idea. – Suhail Gupta Mar 21 '17 at 06:33
  • [CONTD.] I particularly want to send the results back to the client after all the promises are resolved. – Suhail Gupta Mar 21 '17 at 06:38
  • @SuhailGupta - I think I've answered the question you originally asked. If you have a different question about the code you referenced, then you should post a new question, describe the exact problem you're trying to solve, paste the relevant code into the question and describe what you observe and what you want with the code you have. That seems beyond the scope of this question. – jfriend00 Mar 21 '17 at 06:42