Implementing Promises using Bluebird -


i have function needs implemented bluebird promises unable work out. here pseudo code

exports.addemployees=function (req,res){

var data =  [                 {                     firstname: 'xxxxx',                     lastname: 'v',                     phone: '9999999999',                     dateofbirth: '2010-08-02',                     department: 'it',                     startdate: '2015-08-02',                     created: now,                     updated:                 },                 {                     firstname: 'yyyyy',                     lastname: 'k',                     phone: '8888888888',                     dateofbirth: '2011-08-02',                     department: 'it',                     startdate: '2015-08-02',                     created: now,                     updated:                 },             ];    async.each(data, function(item,callback){                      req.db.employee.create(item, callback);                           },function(err){                      if(err){                          res.send("error!");                      }                     res.send("success!");                  }                            ); 

}

thanks

something like

var promise = require("bluebird")  var data =  [                 {                     firstname: 'xxxxx',                     lastname: 'v',                     phone: '9999999999',                     dateofbirth: '2010-08-02',                     department: 'it',                     startdate: '2015-08-02',                     created: now,                     updated:                 },                 {                     firstname: 'yyyyy',                     lastname: 'k',                     phone: '8888888888',                     dateofbirth: '2011-08-02',                     department: 'it',                     startdate: '2015-08-02',                     created: now,                     updated:                 },             ];   promise.map(data, function(item) {     return req.db.employee.create(item)         .then(function(id){ return id })         .catch(myerror, function(e) {             e.item = item;             throw e;         }) }).then(function(idlist) {     res.send("success!"); }).catch(myerror, function(e) {     console.log("operation failed on " + e.item + ": " + e.message);     res.send("error!"); }); 

you need define myerror make work (https://github.com/petkaantonov/bluebird/blob/master/api.md#catchfunction-errorclassfunction-predicate-function-handler---promise)

p.s. sure, req.db.employee.create(item) should support promises, need promisify it: https://github.com/petkaantonov/bluebird/blob/master/api.md#promisepromisifyallobject-target--object-options---object


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -