承诺然后功能没有执行

时间:2015-12-16 12:58:50

标签: javascript promise bluebird

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完美地执行。我究竟做错了什么。我不太确定承诺是如何运作的。我想确保一旦完成db写操作,我就可以在当时的调用中从数据库中读取。

1 个答案:

答案 0 :(得分:2)

当您已经承诺使用时,您无需创建新的承诺。这是一种反模式,在此描述为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; });
                    });                     
                }       
}