node.js chain multiple promises (with mongoose) - javascript

The following is a typical promise function that I am dealing with.
var _delete = function(t, id) {
return Promise.cast(Event.find({where: {id: id}}, {transaction: t}))
.then(function(d){
if (d) {
// ------- (*)
return Promise.cast(d.updateAttributes({status: -1}, {transaction: t}))
.then(function(){
// do inventory stuff
return Promise.cast(Inventory.update({}).exec())
.then(function(d){
// do something
})
}).then(function(){
// do product stuff
return Promise.cast(Product.update({}).exec())
.then(function(d){
// do something
})
})
} else {
return Promise.reject('this transaction list does not exist');
}
});
};
This looks ok until when I am dealing with more complicated update / creates the code will become really messy.
Currently what I am doing with promise is that
1. I have a lot of useless return true statements and the only purpose is to go to next .then statement
2. promise are programmed in a nested style. also the input arguments are usually complicated and has more then 1 arguments so that I cannot do something like this
.then(fun1).then(fun2)
... etc
which makes me unable to 'tap' the .then statement to enable/disable a functionality.
So my questions is how do I do this correctly? Thanks..
the following is the really ugly things that I am talking about....
var _process = function(t, tid) {
var that = this;
return Promise.cast(Usermain.find({where: {transaction_id: tid}}))
.bind({}) // --- (*)
.then(function(d){
this.tmain = d;
return true; // ---- do nothing, just go to next thennable (is this correct)
}).then(function(){
return Promise.cast(Userlist.findAndCountAll({where: {transaction_id: tid}}))
}).then(function(d){
this.tlist = d;
return true; // ---- do nothing, just go to next thennable (is this correct)
}).then(function(){
if (this.tmain.is_processed) {
return Promise.reject('something is wrong');
}
if (this.tlist.count !== this.tmain.num_of_tran) {
return Promise.reject('wrong');
}
return Promise.resolve(JSON.parse(JSON.stringify(this.tlist.rows)))
.map(function(d){
if (d.is_processed) return Promise.reject('something is wrong with tran list');
return true; // goto next then
});
}).then(function(){
return Promise.cast(this.tmain.updateAttributes({is_processed: 1}, {transaction: t}));
}).then(function(){
return Promise.resolve(this.tlist.rows)
.map(function(d){
var tranlist = JSON.parse(JSON.stringify(d));
return Promise.cast(d.updateAttributes({is_processed: 1, date_processed: Date.now()}, {transaction: t}))
.then(function(d){
if (!d) {
return Promise.reject('cannot update tran main somehow');
} else {
if (tranlist.amount < 0) {
return Usermoney._payBalance(t, tranlist.user_id, -tranlist.amount);
} else {
return Usermoney._receiveBalance(t, tranlist.user_id, tranlist.amount);
}
}
});
});
});
}

You can do two things:
Unnest then callbacks
Modularize. These "do product stuff" and "do inventory stuff" things might become their own functions (or even the same?).
In this case, unnesting could do the following (assuming you don't need closures in your commented sections):
function _delete(t, id) {
return Promise.cast(Event.find({where: {id: id}}, {transaction: t}))
.then(function(d){
if (d) {
return Promise.cast(d.updateAttributes({status: -1}, {transaction: t}));
else
throw new Error('this transaction list does not exist');
})
.then(function(){
// do inventory stuff
return Promise.cast(Inventory.update({}).exec())
})
.then(function(d){
// do something
})
.then(function(){
// do product stuff
return Promise.cast(Product.update({}).exec())
})
.then(function(d){
// do something
});
}

In my projects I use Async.js
I think you need to decompose your _process method into small actions
Actions which depend on result from previous actions - async waterfall pattern might be used here
Actions which don't depend on the previous actions result, they may be executed in parallel
Use some custom process
Here is an example from my app:
async.waterfall([
function findUser(next) {
Users.findById(userId, function (err, user){
if(err) {
next(new Error(util.format('User [%s] was not found.', userId)));
return;
}
next(null, user);
});
},
function findUserStoriesAndSurveys(user, next) {
async.parallel([
function findStories(callback) {
// find all user stories
Stories.find({ UserGroups: { $in : user.Groups } })
.populate('Topic')
.populate('Episodes')
.exec(function(err, stories) {
if(err) {
callback(err);
return;
}
callback(null, stories);
});
},
function findSurveys(callback) {
// find all completed surveys
Surveys.find({
User: user
}).exec(function(err, surveys) {
if(err) {
callback(err);
return;
}
callback(null, surveys);
});
}
],
function(err, results) {
if(err) {
next(err);
return;
}
next(null, results[0], results[1]);
});
},
function calculateResult(stories, surveys, next) {
// do sth with stories and surveys
next(null, { /* result object */ });
}
], function (err, resultObject) {
if (err) {
res.render('error_template', {
status: 500,
message: 'Oops! Server error! Please reload the page.'
});
}
res.send(/* .... */);
});
Please refer to Async docs for a custom process, it really does contain a lot of common patterns, I also use this library in my client JavaScript.

Related

Callback if Event is received multiple Times

I'm currently writing a node application, that checks if a certain file exists at a specific location. For every order that exists where it is supposed to be I'd like to make a put request to my Woocommerce Api, that changes the order status to Processing.
for (i=0; i<my_orders.length; i++) {
var exportedThisPdf = true;
var orderId = my_orders[i].orderId.toString();
for (p=0; p<my_orders[i].products.length; p++) {
var stickerId = my_orders[i].products[p].meta[0].value;
if (fs.existsSync('/mypath/test')) {
} else {
exportedThisPdf = false;
}
}
if (exportedThisPdf == true) {
var data = {
status: 'processing'
};
client.updateStatus(orderId, data, function (err) {
if (err) console.log(err);
})
} else {
var data = {
status: 'failed'
};
client.updateStatus(orderId, data, function (err) {
if (err) console.log(err);
})
}
}
console.log("callback");
I would now like to only continue the code once all my order statuses have been successfully updated to either processing or failed.
Is there a way to go about this problem in a clean, asynchronous way?
Thanks in Advance
You want to await some promises. So at first create a global variable:
var promises = [];
Then basically whenever we do sth asynchronous, we add a promise to that array, e.g.:
promises.push(new Promise(function(resolve){
client.updateStatus(orderId, data, function (err) {
if (err) return console.log(err);
resolve();
})
}));
Then if all promises are added, we can await them:
Promise.all(promises)
.then(function(){
console.log("finished");
});
Try for this: Use Async Module
var async = require('async');
async.eachSeries(my_orders, function(order, ordercallback){
async.eachSeries(order.products, function(product, productcallback){
// DO your put logic here
client.updateStatus(orderId, data, function (err) {
productcallback();
})
}, function(err){
if(!err) ordercallback()
});
});

Node.js - How to chain Promise in The right way

I recently moved from callback functions to promises in node.js. I want to preform async query to the DB (psql) in the most elegant way. I was wondering if the following code is the right way to do it or if I can chain for example the promises in a way of first().then(second).then(third).
function queryAll(req, res) {
searchQuery()
.then((rows) => {
console.log(rows);
totalQuery()
.then((total) => {
console.log(total);
});
});
res.json({"rows": rows, "total": total});
}
function searchQuery() {
return new Promise(function(resolve, reject) {
var rowData = { rows: {} };
pool.query('select age, sex from workers;', values, function(err, result) {
if(err) {
return console.error('error running query', err);
reject(err);
}
rowData.rows = result.rows;
resolve(rowData);
});
});
}
function totalQuery() {
return new Promise(function(reject, resolve) {
var totalData = { totals: {} };
pool.query('select sex, scores from workers group by sex;', values, function(err, result) {
if(err) {
return console.error('error running query', err);
reject(err);
}
totalData.totals = result.rows;
resolve(totalData);
});
});
}
var rowData = { rows: {} };
var totalData = { totals: {} };
First of all, these make no sense stored in variables since there's nothing else on the object. Just resolve with the rows directly instead.
return console.error('error running query', err);
Also, don't just console.log your errors. then accepts a second callback that executes on thrown errors or rejected promises. Throw this message in an error or reject with it instead. Also, I would leave logging to the consumer.
function queryAll(req, res) {
searchQuery()
.then((search) => {
console.log(rows);
totalQuery()
.then((total) => {
console.log(total);
});
});
res.json({"rows": rows, "total": total});
}
rows and total don't exist anywhere. Plus, by the time res.json executes, rows and total (assuming they come from inside the callbacks) won't exist yet since the whole sequence is async. You'll get undefined values as results.
I see little point in running searchQuery and totalQuery in sequence as they're not dependent on each other. It's better to run them parallel instead. Use Promise.all for that.
function queryAll(req, res) {
Promise.all([
searchQuery(),
totalQuery()
]).then(values => {
const rows = values[0];
const total = values[1];
res.json({"rows": rows, "total": total});
}, function(e){
// something went wrong
});
}
function searchQuery() {
return new Promise(function(resolve, reject) {
pool.query('select age, sex from workers;', values, function(err, result) {
if(err) reject(err);
else resolve(result.rows);
});
});
}
function totalQuery() {
return new Promise(function(reject, resolve) {
pool.query('select sex, scores from workers group by sex;', values, function(err, result) {
if(err) reject(err);
else resolve(result.rows);
});
});
}
You have a few issues in the code:
You return before executing reject()
There is an undefined rows variable (mismatch with search)
res.json is executed before the results are in.
The promises resolve to objects like { rows: rows }, but the main function seems to expect the plain numbers, not the objects. So let the promises just resolve to numeric values.
The second SQL is ambiguous since the second field is not aggregated and does not appear in the group by clause either. Assuming you want to sum the scores, use sum().
The second query is only launched after the first one has returned results, but this can be done in parallel
You have very similar code repeated. Try to reuse code and make the SQL statement an argument.
Here is how I would suggest to do it:
function queryAll(req, res) {
return Promise.all([searchQuery(), totalQuery()]).then(([rows, total]) => {
console.log('rows', rows);
console.log('total', total);
// Make sure to only access the promised values in the `then` callback
res.json({rows, total});
});
}
function searchQuery() {
return promiseQuery('select age, sex from workers;');
}
function totalQuery() {
// When you group, make sure to aggregate:
return promiseQuery('select sex, sum(scores) as scores from workers group by sex;');
}
function promiseQuery(sql) { // reusable for other SQL queries
return new Promise(function(resolve, reject) {
pool.query(sql, values, function(err, result) {
if(err) {
// Do not return before calling reject!
console.error('error running query', err);
reject(err);
return;
}
// No need for a variable or object, just resolve with the number of rows
resolve(result.rows);
});
});
}
The most elegant solution would be via pg-promise:
function queryAll(req, res) {
db.task(t => {
return t.batch([
t.any('SELECT age, sex FROM workers', values),
t.any('SELECT sex, scores FROM workers GROUP BY sex', values)
]);
})
.then(data => {
res.json({rows: data[0], total: data[1]});
})
.catch(error => {
// handle error
});
}
And that's everything. You don't have to reinvent promise patterns for working with the database, they are all part of the library already.
And if your queries have a dependency, see: How to get results from multiple queries at once.
Or if you prefer ES6 generators:
function queryAll(req, res) {
db.task(function* (t) {
let rows = yield t.any('SELECT age, sex FROM workers', values);
let total = yield t.any('SELECT sex, scores FROM workers GROUP BY sex', values);
return {rows, total};
})
.then(data => {
res.json(data);
})
.catch(error => {
// handle error
});
}
And with the ES7's await/async it would be almost the same.
First of all there are some errors in your code, you have to place the reject before the return, otherwise it will be never called, and create a dangling promise:
function searchQuery() {
return new Promise(function(resolve, reject) {
var rowData = {
rows: {}
};
pool.query('select age, sex from workers;', values, function(err, result) {
if (err) {
reject(err);
console.error('error running query', err);
} else {
rowData.rows = result.rows;
resolve(rowData);
}
});
});
}
Beside that you should not nest the Promises when ever possible.
So it should be:
function queryAll(req, res) {
var result = {};
searchQuery()
.then((search) => {
console.log(search);
result.rows = search;
return totalQuery();
})
.then((total) => {
result.total = total;
console.log(total);
});
}
The res.json has to be called in the then part of the Promise:
function queryAll(req, res) {
var result = {};
searchQuery()
.then((search) => {
console.log(search);
result.rows = search;
return totalQuery();
})
.then((total) => {
result.total = total;
console.log(total);
})
.then(() => {
res.json({
"rows": result.rows,
"total": result.total
});
});
}
If your queryAll is called as e.g. middleware of express, then you should handle the catch case within queryAll:
function queryAll(req, res) {
var result = {};
searchQuery()
.then((search) => {
console.log(search);
result.rows = search;
return totalQuery();
})
.then((total) => {
result.total = total;
console.log(total);
})
.then(() => {
res.json({
"rows": result.rows,
"total": result.total
});
})
.catch( err => {
res.status(500).json({error: 'some error'})
});
}
For postgress I would suggest to use pg-promise instead of using a callback style library and wrapping it into promises yourself.
You could simplify the code if you use a library like bluebird:
const bPromise = require('bluebird')
function queryAll(req, res) {
bPromise.all([
searchQuery(),
totalQuery()
])
.spread((rows, total) => {
res.json({
"rows": rows,
"total": total
});
})
.catch(err => {
res.status(500).json({
error: 'some error'
})
});
}
With nsynjs your logic may be coded as simple as this:
var resp = {
rows: dbQuery(nsynjsCtx, conn, 'select age, sex from workers', values1).data,
total: dbQuery(nsynjsCtx, conn, 'select sex, scores from workers group by sex', values2).data
};
Please see example of multiple sequential queries here: https://github.com/amaksr/nsynjs/tree/master/examples/node-mysql

Node Promise - TypeError cannot read property .then of undefined

The two pieces of code below throw a type error:
TypeError: Cannot read property 'then' of undefined.
I feel like I'm missing something fundamental. Remarkable is that the log of 'result' in the second piece of code is done after the error is thrown. Which leads me to believe I might be doing something wrong involving asynch. However I cannot get my head around it, even after reading the suggested questions.
Any help would be greatly appreciated!
router.route('/user/:id')
.put(auth.authRest, function(req, res) {
userManager.updateUser(req.params.id, req.body)
.then(function(response) { // line where error is thrown
res.send({data:response});
});
});
and from userManager:
this.updateUser = function(id, data) {
User.findOne({ _id: id }).exec(function(err, user){
if(err) {
console.log(err);
} else {
for(let prop in data) {
user[prop] = data[prop];
}
var result = user.save().catch(function(err){
console.log(err);
});
console.log(result); // this log is done below the error, it does contain a promise
return result;
}
}).catch(function(err){
console.log(err);
});
};
If you want to use Promises you need to return a Promise from this.updateUser, the return result belongs to the callback you pass to exec and not to the function you assigned to this.updateUser.
this.updateUser = function(id, data) {
return User.findOne({
_id: id
}).exec().then(function(user) {
for (let prop in data) {
user[prop] = data[prop];
}
var result = user.save().catch(function(err) {
console.log(err);
});
console.log(result); // this log is done below the error, it does contain a promise
return result;
}).catch(function(err) {
console.log(err);
});
};
Depending on how you want to do the error handling you could shrink it down to:
this.updateUser = function(id, data) {
return User.findOne({
_id: id
}).exec().then(function(user) {
for (let prop in data) {
user[prop] = data[prop];
}
return user.save();
}).catch(function(err) {
console.log(err);
});
};
'updateUser' method should return a promise, so that .then call in the first method would work.
Try something like below ( used node package 'q')
this.updateUser = function(id, data) {
var deferred = Q.defer()
User.findOne({ _id: id }).exec(function(err, user){
if(err) {
console.log(err);
deferred.reject(err)
} else {
for(let prop in data) {
user[prop] = data[prop];
}
var result = user.save().catch(function(err){
console.log(err);
});
console.log(result); // this log is done below the error, it does contain a promise
deferred.resolve(resolve)
//return result;
}
}).catch(function(err){
deferred.reject(err)
console.log(err);
});
return deferred.promise
};

Issue with promises in a for loop

I'm confronted to a situation which drives me a bit mad.
So The situation is as below :
module.exports = {
generation: function (req, res) {
// Let's firstly fetch all the products from the productTmp Table
function fetchProductsTmp (){
ProductsTmp.find().then(function (products) {
return Promise.all(products.map (function (row){
Service.importProcess(row);
}));
});
}
fetchProductsTmp();
}
Here I simply call my model ProductsTmp to fetch my datas and iterate through my rows calling importProcess.
importProcess :
importProcess: function (product) {
async.series([
function (callback) {
return SousFamille.findOne({name: product.sous_famille}).then(function (sf) {
console.log('1');
if (!sf) {
return SousFamille.create({name: product.sous_famille}).then(function (_sf) {
console.log('2');
callback(null, _sf.sf_id);
});
} else {
callback(null, sf.sf_id);
}
});
},
function (callback){
console.log('3');
},
], function(err, results){
if(err) return res.send({message: "Error"});
});
}
So I got with my console log :
1
1
1
2
3
2
3
2
3
What I want to Obtain is 1 2 3 1 2 3 1 2 3 So that each function wait for the promise to finish before calling the next one.
In the generation function of the first section, replace
return Promise.all(products.map (function (row){
Service.importProcess(row);
}));
with
var results = [],
pushResult = id => results.push(id);
return products.reduce(function(prev, row){//Go through all the products
//Take the previous promise, and schedule next call to Service.importProcess to be
//made after the previous promise has been resolved
return prev.then(function(){
return Service.importProcess(row).then(pushResult);
});
}, Promise.resolve())
.then(() => results);
You also need to return a promise from importProcess for this to work. Just ditch the whole async.series thingy and do something like
return new Promise(function(resolve, reject){
...
resolve(sf.sf_id); //instead of the callback(null, sf.sf_id)
...
});
Update: This forces the calls to Service.importProcess to be sequential instead of concurrent, which does affect the overall performance of calls to generation. But I guess you have more solid reasons to do so than sequential console.logs.
Sorry, can't help the urge to do it in ES6, basically things can be reduced to single line, like Bergi said, async is redundant( using Bluebird Promise Library):
importProcess: product =>
SousFamille.findOne({name: product.sous_famille})
.then(sf => sf? sf.sf_id : SousFamille.create({name: product.sous_famille}).then(_sf => _sf.sf_id))
// the other module
module.exports = {
generation: (req, res) => ProductsTmp.find()
.then(products => Promise.mapSeries(products, Service.importProcess.bind(Service)) )
.then(ids => res.send({ids}))
.catch(error => res.send({message: 'Error'}))
}
also like noppa said, your problem is the missing return in Service.importProcess(row), same code in ES5:
module.exports = {
generation: function (req, res) {
ProductsTmp.find()
.then(function (products) {
return Promise.mapSeries(products, Service.importProcess.bind(Service)) );
}).then(function(ids){
res.send({ids: ids});
}).catch(function(error){
res.send({message: 'Error'});
})
}
importProcess: function (product) {
return SousFamille.findOne({name: product.sous_famille})
.then(function (sf) {
if (sf) return sf.sf_id;
return SousFamille.create({name: product.sous_famille})
.then(function (_sf){ return _sf.sf_id});
});
}

How can I wrap an async call in Javascript?

I want to have three functions that do something like this:
function getuser1(){
var user = b();
//do some stuff to user
return user;
}
function getuser2(){
var user = asyncfinduser();
//do some different stuff to user
return user;
}
function asyncfinduser(){
userDB.find( { /* some criteria */ }, function (error, user) {
//return found user to functions above
return user;
});
)
The above is obviously not going to work so how can I fix this? Thank you
Since you cannot return synchronously from async callbacks, you will need to use callbacks:
function getuser1(callback) {
asyncfinduser(function(error, user) {
if (error) return callback(error);
//do some stuff to user
callback(null, user);
});
}
function getuser2(callback) {
asyncfinduser(function(error, user) {
if (error) return callback(error);
//do some different stuff to user
callback(null, user);
});
}
function asyncfinduser(callback) {
userDB.find( { /* some criteria */ }, function (error, user) {
if (error) return callback(error);
//return found user to functions above
callback(null, user);
});
}
However, you might be able to apply the promise pattern:
var Promise = …; // some library
function getuser1() {
return asyncfinduser().then(function(user) {
//do some stuff to user
return user;
});
}
function getuser2() {
return asyncfinduser().then(function(user) {
//do some different stuff to user
return user;
});
}
function asyncfinduser() {
return new Promise(function(resolve, reject) {
userDB.find( { /* some criteria */ }, function (error, user) {
if (error) reject(error);
else resolve(user); //return found user to functions above
});
}

Categories