David
David

Reputation: 3

Bluebird Promise.all callback error

im trying to process an array using Promise.all(), but i can't, here is the code:

this function request info a imdb

module.exports = {

info: function(name, fn){

  var MovieDB = require("moviedb")("470fd2ec8853e25d2f8d86f685d2270e"),
    path = require("path");

  var rex = /([a-z]*\b(?:m2ts|mp3|sub|spanish|english|french|brrip|gaz|yify|avi|mp4|mkv|bdrip|dvdrip|dvdscr|dvdscreener|screener|dvdrip|r5|telesync|480p|480i|576p|576i|720p|720i|1080p|1080i|hrhd|hrhdtv|hddvd|bluray|x264|h264|xvid|ac3|dts|aac))/ig
  name = path.basename(name)
  name = name.replace(rex, "");

  var regexNameYear = /(.+?)\W?(\d{4})[^1080|720]/ig;
  var tvShowPattern = /(s[0-9][0-9])|(se[0-9][0-9])|(season\.[0-9])|(season [0-9])|(season[0-9])|(season [0-9][0-9])|([0-9]x)/ig;
  var year = /(\d{4})/g

  var hasYear;
  var yearFile;

  if(name.match(/\d{4}/g)){
    yearFile = name.match(/\d{4}/g);
    yearFile = yearFile[0];
    hasYear = true;
  }

  mov = name.match(regexNameYear);

  if(mov != null){
    mov = mov[0].replace(/\./g, " ");
    mov = mov.replace(/(\d{4})/g, "")
  }else{
    mov = name.match(/([A-Z])\w+/ig);
    mov = mov.join(" ");
  }

  var p

  MovieDB.searchMulti({query: mov }, function(err, res){

    var info = res.results[0]

    if(info != undefined){

      p = { id: info.id,
             backdrop_path: info.backdrop_path,
             original_title: info.original_title,
             poster_path: info.poster_path,
             popularity: info.popularity
           }

    return fn(err, p)
   }
  });
 }
}

Here im trying to use the promises:

var arp = []
arp.push(posters.info('american.horror.story.2014.s03e01.bitchcraft.720p.web-dl.sujaidr.mkv')
arp.push(posters.info('Autómata.2014.720p.BluRay.x264.YIFY.mp4'))
arp.push(posters.info('Evil.Dead.2013.720p.BluRay.x264.YIFY.mp4'))
arp.push(posters.info('Deliver.Us.from.Evil.2014.720p.BluRay.x264.YIFY.mp4'))
arp.push(posters.info('Falling.Skies.2013.S01E01E02.720p.HDTV.x264.mkv'))


Promise.all(arp).then(function(p){
  console.log(p);
})

But i have this result:

movieInfo.js:56
    return fn(err, p)
           ^
TypeError: undefined is not a function

What im doing wrong?

regards.

Upvotes: 0

Views: 127

Answers (1)

Bergi
Bergi

Reputation: 664327

Don't use callback functions any more if you want to work with promises. Instead of taking a fn, return a promise from your info method! (That's what Promise.all expects as well, an array of promises)

So promisify your lib:

var Promise = require("bluebird"),
    MovieDB = Promise.promisiyAll(require("moviedb")("470fd2ec8853e25d2f8d86f685d2270e"));
module.exports = {
  info: function(name) {
    …
    return MovieDB.searchMultiAsync({query: mov})
//  ^^^^^^                    ^^^^^
    .then(function(res) {
      var info = res.results[0]
      if (info != undefined)
        return {
          id: info.id,
          backdrop_path: info.backdrop_path,
          original_title: info.original_title,
          poster_path: info.poster_path,
          popularity: info.popularity
        };
    });
  }
}

Upvotes: 2

Related Questions