Bluebird Promise.all回调错误

时间:2015-05-05 01:38:56

标签: javascript promise bluebird

我试图使用Promise.all()来处理数组,但我不能,这是代码:

此函数请求信息imdb

W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
D/ISystemWriteService(  115): readSysfs could not contact remote
D/ISystemWriteService(  115): readSysfs could not contact remote
D/ISystemWriteService(  115): readSysfs could not contact remote
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
I/amplayer( 6310): **[update_state]pid:26 status=PLAYING(last:PLAYING) err=0x0 curtime=4 (ms:4473) fulltime=30 lsttime=4
I/amplayer( 6310): **[update_state]abuflevel=0.18177669 vbublevel=0.34421411 abufrp=9871880 vbufrp=7b46200 read_end=1
V/AmlogicPlayer( 6310): update_process pid=26, current=4,status=[PLAYING]
V/AmlogicPlayer( 6310): Playing percent =13,mPlayTime:4473,mStreamTime:4473
V/AmlogicPlayer( 6310): Playing percent on percent=99,bufed pos=7223869,Duration=7249275
V/AmSuperPlayer( 6310): [notify::498]
V/AmSuperPlayer( 6310): [Notify::505]
V/AmSuperPlayer( 6310): cookie=0xb7307d40,msg=3,ext1=63,ext2=0
V/MediaPlayerService( 6310): [27] notify (0xb723d660, 3, 99, 0)
D/ISystemWriteService(  115): readSysfs could not contact remote
V/AmSuperPlayer( 6310): [notify::498]
V/AmSuperPlayer( 6310): [Notify::505]
V/AmSuperPlayer( 6310): cookie=0xb7307d40,msg=c8,ext1=9001,ext2=0
V/MediaPlayerService( 6310): [27] notify (0xb723d660, 200, 36865, 0)
I/AmlogicPlayer( 6310): Got amplayer download speed : 0 kbps
D/ISystemWriteService(  115): readSysfs could not contact remote
D/ISystemWriteService(  115): readSysfs could not contact remote
W/MediaPlayer( 7190): info/warning (36865, 0)
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
I/amplayer( 6310): **[update_state]pid:26 status=PLAYING(last:PLAYING) err=0x0 curtime=5 (ms:5007) fulltime=30 lsttime=4
I/amplayer( 6310): **[update_state]abuflevel=0.17787044 vbublevel=0.34024277 abufrp=9873080 vbufrp=7b55600 read_end=1
V/AmlogicPlayer( 6310): update_process pid=26, current=5,status=[PLAYING]
V/AmlogicPlayer( 6310): Playing percent =14,mPlayTime:5007,mStreamTime:5007
V/AmlogicPlayer( 6310): Playing percent on percent=99,bufed pos=7223869,Duration=7249275
V/AmSuperPlayer( 6310): [notify::498]
V/AmSuperPlayer( 6310): [Notify::505]
V/AmSuperPlayer( 6310): cookie=0xb7307d40,msg=3,ext1=63,ext2=0
V/MediaPlayerService( 6310): [27] notify (0xb723d660, 3, 99, 0)
V/AmSuperPlayer( 6310): [notify::498]
V/AmSuperPlayer( 6310): [Notify::505]
V/AmSuperPlayer( 6310): cookie=0xb7307d40,msg=c8,ext1=9001,ext2=0
V/MediaPlayerService( 6310): [27] notify (0xb723d660, 200, 36865, 0)
I/AmlogicPlayer( 6310): Got amplayer download speed : 0 kbps
W/MediaPlayer( 7190): info/warning (36865, 0)
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers
W/AudioFlinger( 6310): OutputTrack::write() 0xb36e8008 thread 0xb6ffebc8 no more overflow buffers

这里我试图使用承诺:

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)
   }
  });
 }
}

但我有这个结果:

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);
})

我做错了什么?

问候。

1 个答案:

答案 0 :(得分:2)

如果您想使用promises,请不要再使用回调函数。而不是取fn,而是从info方法返回一个承诺! (这也是Promise.all所期望的,一系列承诺)

所以promisify你的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
        };
    });
  }
}