Bluebird Promise.all 回调错误
Bluebird Promise.all callback error
我正在尝试使用 Promise.all() 处理数组,但我不能,这是代码:
此函数请求信息 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)
}
});
}
}
我在这里尝试使用承诺:
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);
})
但我得到了这个结果:
movieInfo.js:56
return fn(err, p)
^
TypeError: undefined is not a function
我做错了什么?
问候。
如果您想使用 promise,请不要再使用回调函数。而不是从您的 info
方法中获取 fn
、return 承诺! (这也是 Promise.all
所期望的,一系列承诺)
所以promisify你的库:
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
};
});
}
}
我正在尝试使用 Promise.all() 处理数组,但我不能,这是代码:
此函数请求信息 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)
}
});
}
}
我在这里尝试使用承诺:
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);
})
但我得到了这个结果:
movieInfo.js:56
return fn(err, p)
^
TypeError: undefined is not a function
我做错了什么?
问候。
如果您想使用 promise,请不要再使用回调函数。而不是从您的 info
方法中获取 fn
、return 承诺! (这也是 Promise.all
所期望的,一系列承诺)
所以promisify你的库:
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
};
});
}
}