Promises with _.each 异步调用

Promises with _.each async calls

var TheMovieDb = require('themoviedb');
var moviedbClient = new TheMovieDb('*****');
var movieJson = require("./seedMovieDB/movieName.json");
var MovieEntry = require('./movie.model');
var movieApi = new TheMovieDb('1b3819c5f61aaef99edf4c47a5de46f4', 'en');
var P = require('bluebird');
var _ = require('underscore');
var moviedb = module.exports = {
indexMovie : function indexMovie(){
            MovieEntry.removeAsync({})
            .then (function() {
                _.each(movieJson, function(val, key, cb){
                            movieApi.searchMovies(val.name).then (function(mDetails) {
                            if(mDetails !== undefined){
                                _.each(mDetails , function(val, key, cb){
                                        var m = new MovieEntry({
                                            id: val.id,
                                            title : val.title,
                                            originalTitle : val.originalTitle,
                                            year : val.year,
                                            popularity : val.popularity,
                                            voteAverage : val.voteAverage,
                                            votes : val.votes,
                                            isAdult : val.isAdult,
                                            video : val.video,
                                            poster : val.poster,
                                            backdrop : val.backdrop,                        
                                        });
                                        m.save(function(err, movie_saved){
                                            if(err){
                                                console.log(err);
                                            }else{
                                                console.log("saved");
                                            }
                                        }); 
                                })
                            }                       
                        }).catch(function(err){
                            if(err){
                                console.log(err);
                            }
                        });
                });
        });
}

}

我想要 return 一个承诺或其他东西,这将确保一旦我的所有调用,即异步搜索电影调用结束,然后我可以使用 .then() 来检索我在代码中存储的数据库中的东西。 我是新手,不知道该怎么做。 我有一个调用 indexMovie 函数的控制器,调用结束后我想从数据库中检索保存的值。

只需在每个

的末尾添加 promise().done(function(){
var moviedb = module.exports = {
indexMovie : function indexMovie(){
            MovieEntry.removeAsync({})
            .then (function() {
                _.each(movieJson, function(val, key, cb){
                            movieApi.searchMovies(val.name).then (function(mDetails) {
                            if(mDetails !== undefined){
                                _.each(mDetails , function(val, key, cb){
                                        var m = new MovieEntry({
                                            id: val.id,
                                            title : val.title,
                                            originalTitle : val.originalTitle,
                                            year : val.year,
                                            popularity : val.popularity,
                                            voteAverage : val.voteAverage,
                                            votes : val.votes,
                                            isAdult : val.isAdult,
                                            video : val.video,
                                            poster : val.poster,
                                            backdrop : val.backdrop,                        
                                        }).promise().done(function(){
                                             //the stuff you need
                                        });
                                        m.save(function(err, movie_saved){
                                            if(err){
                                                console.log(err);
                                            }else{
                                                console.log("saved");
                                            }
                                        }); 
                                })
                            }                       
                        }).catch(function(err){
                            if(err){
                                console.log(err);
                            }
                        });
                });
        });
}

使用 Promise.each 而不是 _.each 来等待异步操作。如果操作不相关,请改用 Promise.map 以便它们可以同时执行。

首先 - 正确的解决方案是让您的数据库调用批量执行查询,而不是针对 100 件事查询 API 100 次,而是查询一次。 movieApi.searchMovies(val.name) 应该有一个 movieApi.searchMovies(...arrr) 替代方案,适用于多个值等。

也就是说,主要部分变成了:

return Promise.map(movieJson, x => searchMovies(x.name)).map(x =>
    new MovieEntry(val);
).map(x =>
    x.save()
).then(x =>
    console.log("All Done!")
).catch(e => {
    console.error("Error somewhere", e);
    throw e;
});