javascript - Are bluebird promises blocking in nature -


i may bit new understand underlying functioning of promises esp bluebird. trying accomplish api server processes bulk write of 250k+ rows database. takes around 30s complete request. want api server return ok or error based on success of bulk write. when don't use promises don't opportunity bubble error because request not waiting bulk write complete. however, if use promise error , success works properly. server becomes unresponsive till action complete. in nutshell, using promise library handle bulk write blocks api server.

function chunk(arr, chunksize) {   var r = [];    (var i=0,len=arr.length; i<len; i+=chunksize)     {         r.push(arr.slice(i,i+chunksize));     }   return promise.resolve(r); }   exports.add = function(req, res) { var po_std_lt_time = 90; //days     parts.sync()         .then(function() {             return parts.destroy({                 where: {}             });         })         .then(function() {             var workbook = xlsx.readfilesync(__dirname + '/dbo_tblcplparts_.xlsx');             var sheet_name_list = workbook.sheetnames;             var json_s = xlsx.utils.sheet_to_json(workbook.sheets[sheet_name_list[0]]);              var size = 40000;             chunk(json_s, size).then(function(json_small) {                 promise.each(json_small, function (json_small_){                     parts.bulkcreate(json_small_)                     .catch(function(err) {                         res.json(500, {                             error: "error : " + err                         });                     })                  }).finally(ext_fns.handleresult(res,200))             })             })  } 

what best way handle this? using promise right way?

it's because you're invoking synchronous behavior along way. culprit is

var workbook = xlsx.readfilesync(__dirname + '/dbo_tblcplparts_.xlsx'); 

you should instead use xlsx equivalent of fs.readfile, asynchronous. alternatively, since you're using bluebird, can use promisifyall on xlsx module:

var promise = require('bluebird') var fs = promise.promisifyall(/* xlsx module */) 

which allow treat xlsx modules promises.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -