Promise then 函数未执行
Promise then function not executed
exports.index = function(req, res) {
moviedb.indexMovie().then(function(){
console.log("READ VALUES FROM DATABASE");
});
};
var moviedb = module.exports = {
indexMovie : function() {
return new P(function(resolve, reject){
MovieEntry.removeAsync({})
.then (function() {
return P.map(movieJson, x => movieApi.searchMovies(x.name)).map(x => {
return new MovieEntry({
id: x.id,
title : x.title,
originalTitle : x.originalTitle,
year : x.year,
popularity : x.popularity,
voteAverage : x.voteAverage,
votes : x.votes,
isAdult : x.isAdult,
video : x.video,
poster : x.poster,
backdrop : x.backdrop,
});
}).map(x => x.save())
.then(x => console.log("All Done!"))
.catch(e => { console.error("Error somewhere", e); throw e; });
})
})
}
}
我从来没有看到日志- "READ FROM DATABASE"。但是函数 indexMovie 执行得很好。我究竟做错了什么。我不太确定这个承诺是如何运作的。我想确保一旦数据库写入完成,我就可以在 then 调用中从数据库中读取。
如果您已经有一个可以使用的承诺,则无需创建新的承诺。这是此处描述为 deferred antipattern 的反模式。
您应该改用 removeAsync()
函数提供的承诺,让承诺链正常执行。
exports.index = function(req, res) {
moviedb.indexMovie().then(function(){
console.log("READ VALUES FROM DATABASE");
});
};
var moviedb = module.exports = {
indexMovie : function() {
return MovieEntry.removeAsync({})
.then (function() {
return P.map(movieJson, x => movieApi.searchMovies(x.name)).map(x => {
return new MovieEntry({
id: x.id,
title : x.title,
originalTitle : x.originalTitle,
year : x.year,
popularity : x.popularity,
voteAverage : x.voteAverage,
votes : x.votes,
isAdult : x.isAdult,
video : x.video,
poster : x.poster,
backdrop : x.backdrop,
});
}).map(x => x.save())
.then(x => {console.log("All Done!"); return; })
.catch(e => { console.error("Error somewhere", e); throw e; });
});
}
}
exports.index = function(req, res) {
moviedb.indexMovie().then(function(){
console.log("READ VALUES FROM DATABASE");
});
};
var moviedb = module.exports = {
indexMovie : function() {
return new P(function(resolve, reject){
MovieEntry.removeAsync({})
.then (function() {
return P.map(movieJson, x => movieApi.searchMovies(x.name)).map(x => {
return new MovieEntry({
id: x.id,
title : x.title,
originalTitle : x.originalTitle,
year : x.year,
popularity : x.popularity,
voteAverage : x.voteAverage,
votes : x.votes,
isAdult : x.isAdult,
video : x.video,
poster : x.poster,
backdrop : x.backdrop,
});
}).map(x => x.save())
.then(x => console.log("All Done!"))
.catch(e => { console.error("Error somewhere", e); throw e; });
})
})
}
}
我从来没有看到日志- "READ FROM DATABASE"。但是函数 indexMovie 执行得很好。我究竟做错了什么。我不太确定这个承诺是如何运作的。我想确保一旦数据库写入完成,我就可以在 then 调用中从数据库中读取。
如果您已经有一个可以使用的承诺,则无需创建新的承诺。这是此处描述为 deferred antipattern 的反模式。
您应该改用 removeAsync()
函数提供的承诺,让承诺链正常执行。
exports.index = function(req, res) {
moviedb.indexMovie().then(function(){
console.log("READ VALUES FROM DATABASE");
});
};
var moviedb = module.exports = {
indexMovie : function() {
return MovieEntry.removeAsync({})
.then (function() {
return P.map(movieJson, x => movieApi.searchMovies(x.name)).map(x => {
return new MovieEntry({
id: x.id,
title : x.title,
originalTitle : x.originalTitle,
year : x.year,
popularity : x.popularity,
voteAverage : x.voteAverage,
votes : x.votes,
isAdult : x.isAdult,
video : x.video,
poster : x.poster,
backdrop : x.backdrop,
});
}).map(x => x.save())
.then(x => {console.log("All Done!"); return; })
.catch(e => { console.error("Error somewhere", e); throw e; });
});
}
}