dijs / wiki

Wikipedia Interface for Node.js
MIT License
315 stars 61 forks source link

Problems with promises when the page doesn't have images #11

Closed emilioriosvz closed 8 years ago

emilioriosvz commented 8 years ago

First of all one simple example:

var Wiki = require('wikijs')
var wiki = new Wiki()
var when = require('when')

var searchImages = function (term) {
  var deferred = when.defer()

  wiki.page(term).then(function(page) {
    page.images().then(function(img) {
      // if wikipedia has no images, never go here
      return deferred.resolve(img)
    })
  })

  return deferred.promise
}

searchImages("potato").then(function (response) {
  console.log(response)
})

Suppose that potato has no images on wikipedia, in this case, my promise was never solve.

I had problems looking for some images at a time and when I stood waiting for the promises to be resolved, the application stops because some Wikipedia entries doesn't have images

How can I solve this?

dijs commented 8 years ago

There was a bug with the images method. Thanks for finding that! I have fixed it. And I will push out a new npm module version soon.

Here is how I would create your image search method.

const searchImages = term => wiki.page(term).then(page => page.images());

Then use:

searchImages('batman').then(images=> [do whatever])