有时在promise chain nodejs

时间:2017-11-15 07:32:42

标签: javascript node.js asynchronous callback promise

使用保证链时数据丢失。

基本上,我有4个基于promise的函数,它们都在循环中运行了近15000次迭代,所有4个函数在前一个promise函数被解析后作为解析函数运行。

我的代码流

  1. 从我的数据库中获取max_id以了解否。迭代(基本上是我的数据库中的整部电影)。

  2. 使用参数getMovie()调用函数movie_id,处理来自数据库的数据并将数据保存在movie_data中(movie_data是我对所有承诺的最终响应)并保存数据film_basic_detail_db_resp变量因为某些处理需要在我的其他函数中完成,即otherMovieDetailsSync

  3. 解析getMovie()后,使用参数getCastCrewArray()

  4. 致电movie_id
  5. getCastCrewArray内对我的新数据进行一些处理,并将其保存在movie_data中,与来自getMovie()的数据无关,即film_basic_detail_db_resp

    < / LI>
  6. 解决getCastCrewArray getFilmCreditArray movie_id movie_data后,处理一些数据并将其保存在film_basic_detail_db_resp obj中,不在getFilmCreditArray处理。

  7. 在使用参数otherMovieDetailsSync()解析movie_id来电film_basic_detail_db_resp后,我在getMovie()使用了我在film_basic_detail_db_resp设置的变量otherMovieDetailsSync }

  8. 问题

    很多时候我没有将var Promise = require('promise'); var qryBldr = require("../base/querybuilder"); var no_data = true; var film_basic_detail_db_resp = {}; release_date = ""; movie_data = { "data":{ "mainContainerData":{ } } }; function getFilmCreditArray(movie_id) { return new Promise(function (resolve, reject) { var sp_params = [ movie_id ] resp_obj = {} resp_obj["credits"] = [] credit_obj = { // "credit type":[] } data_val_arr = [] qryBldr.callProcedure('GET_FILM_CREDIT', sp_params,function(err,db) { try { credit_obj = [] db.forEach(function(elem,i) { // process data from my db result // save it into credit_obj //console.log(JSON.stringify(credit_obj)); }); // save data in my movie_data movie_data["data"]["mainContainerData"].push(credit_obj); resolve(movie_id); } catch(err){ reject(err); } }); }); } function otherMovieDetailsSync(movie_id) { return new Promise(function(resolve,reject) { // processing and using the film_basic_detail_db_resp global variable which i saved at getMovie() for further processing // save data to movie_data obj // console.log(JSON.stringify(movie_data)); film_basic_detail_db_resp = undefined; // some dummy value after the processing is done resolve(movie_data); }) } function getCastCrewArray(movie_id) { return new Promise(function(resolve, reject) { var sp_params = [ movie_id ] resp = []; cast_crew_rows = []; crew_data_val_array = []; crew_parsing_obj = {}; qryBldr.callProcedure('GET_FILM_CAST', sp_params,function(err,db) { try{ // console.log(err); // console.log(db); db.forEach(function(elem,i) { // process data from db and put them into cast_crew_rows,crew_data_val_array,crew_parsing_obj }); // save data to movie_data movie_data["data"]["mainContainerData"].push({ "type" : "multiple_column_table_1", "data" : { "isDataHeaderAvailable" : true, "dataValues" : cast_crew_rows } }); } catch(err){ console.log(err); console.log("err"); reject(err); } resolve(movie_id); }); }) } function getMovie(movie_id) { return new Promise(function(resolve,reject) { no_data = true; qryBldr.callProcedure('GET_FILM_DETAILS', [movie_id], function(err, data) { // call to my stoored procedure and getting response from db // console.log(err); // film_basic_detail_db_resp = data; // console.log(film_basic_detail_db_resp); data.forEach(function(elem) { // console.log(film_basic_detail_db_resp); // process results and put data in a global variable film_basic_detail_db_resp, because i want to use it in another function i.e. otherMovieDetailsSync no_data = false; film_basic_detail_db_resp = JSON.parse(JSON.stringify(elem)); // copying data into variblae // fetching data from elem variable for further processing // saving data ro movie_data obj, which is my another global variable movie_data["data"]["mainContainerData"].push({ "type" : "table_without_border_1", "data" : { "dataValues" : box_ofc_data_val_arra, "isNameAvailable" : true, "name" : "Details" } }) resolve(movie_id); }); }); }); } /*getCastCrewArray(1) .then(function(id) { console.log("success obj"); console.log(JSON.stringify(movie_data)); })*/ function onMovieSuccess(md) { // console.log("inside success"); console.log(JSON.stringify(md)); return movie_data } // for(var i = 1; i<900;i++) // {getMovie(i) // // .then(getCastCrewArray) // // .then(getFilmCreditArray) // .then(otherMovieDetailsSync) // .then( // onMovieSuccess, // function() { console.log("rejected"); } // ) // .catch(function(e) { // console.log(e); // })} module.exports = { getMovie:getMovie, getCastCrewArray:getCastCrewArray, getFilmCreditArray:getFilmCreditArray, otherMovieDetailsSync:otherMovieDetailsSync } 内的数据保存在function syncAllMovies(currentIterationID, maxIterationID){ if (maxIterationID == 0) { qryBldr.callProcedure('GET_MAX_FILM_ID', [], function(err, data) { maxIterationID = data[0].max_id; syncMovie.getMovie(currentIterationID) .then(syncMovie.getCastCrewArray) .then(syncMovie.getFilmCreditArray) .then(syncMovie.otherMovieDetailsSync) .then( function(movie_data) { mongoOBJ.getMongoDBObject(function(err, db) { if(err) console.log(err); db.collection("<collection_name>").save(movie_data, function(err, res) { if (currentIterationID + 1 <= maxIterationID) { syncAllMovies(currentIterationID + 1, maxIterationID); } }); }) }, function() { console.log("rejected"); if (currentIterationID + 1 <= maxIterationID) { console.log("deleted"); syncAllMovies(currentIterationID + 1, maxIterationID); } } ) .catch(function(e) { console.log(e); }) }); } else { syncMovie.getMovie(currentIterationID) .then(syncMovie.getCastCrewArray) .then(syncMovie.getFilmCreditArray) .then(syncMovie.otherMovieDetailsSync) .then( function(movie_data) { mongoOBJ.getMongoDBObject(function(err, db) { if(err) console.log(err); db.collection("<collection_name>").save(movie_data, function(err, res) { if (currentIterationID + 1 <= maxIterationID) { syncAllMovies(currentIterationID + 1, maxIterationID); } db.close(); }); }, function(obj) { console.log(obj); if (currentIterationID + 1 <= maxIterationID) { console.log("deleted"); syncAllMovies(currentIterationID + 1, maxIterationID); } } ) .catch(function(e) { console.log(e); }) } syncAllMovies(1, 0); 函数中,我不知道它为什么会发生,尝试了很多东西,没有任何工作。

    我的代码出了什么问题?

    我的实际代码

    SELECT recording_artist.artist_name AS "Artist Name", Musical_genre.musical_genre AS "Genre",
            COUNT(Song.song_id) AS "Number of Songs"                         
    FROM Album
    JOIN recording_artist
        ON recording_artist.recording_artist_id = album.recording_artist_id
    JOIN musical_genre
        ON musical_genre.musical_genre_id = album.musical_genre_id
    JOIN Song 
        ON Album.album_id = Song.album_id                                                              
    GROUP BY recording_artist.artist_name, Musical_genre.musical_genre      
    ORDER BY "Number of Songs" ASC
    

    调用我的代码

    information_schema

    这是1200行的长代码,我将其删除以便解释,如果您有任何疑惑,请随时询问。

0 个答案:

没有答案