shiv
shiv

Reputation: 393

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

I never get to see the log- "READ FROM DATABASE". However the function indexMovie executes perfectly. What am I doing wrong. I am not too sure about how the promise is working. I want to ensure that once the db writes are done i can read from database in the then call.

Upvotes: 0

Views: 1247

Answers (1)

Daniel B
Daniel B

Reputation: 8879

There is no need for you to create a new promise when you already have a promise to work with. This is an anti-pattern described here as the deferred antipattern.

You should instead work with the promise you're given from the removeAsync() function and let the promise chain execute as normal.

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

Upvotes: 2

Related Questions