function addSomething(data) {
var defer = q.defer();
data = _.map(data, function(item) {
item['something'] = callSomethingAsync();
return item;
});
return defer.promise;
}
How can I handle this problem. The only way I found is using Async.js.
But maybe there is a better way using $q?
EDIT:
function getScopes(item) {
var defer = q.defer();
var query = "SELECT somevalue FROM Something WHERE ID = '" + item.id + "'";
mysql.query(query, function(err, data) {
if (err) {
defer.reject(err);
} else {
item[newkey] = data
defer.resolve(item);
}
});
defer.resolve(data)
return defer.promise;
}
//add necessary scopes to the audit
function addScopes(data) {
var promises = _.map(data, function(item) {
return getScopes(item);
});
return Promise.all(promises);
}
How I can prevent using defer in the getScopes function?
Edit 2:
var query = "SELECT * FROM tiscope";
Q.nfcall(mysql.query, query).then(function(data) {
console.log(data);
});
there is nothing returned.
Here is how I use mysql:
var sql = require('mysql');
var connection = sql.createConnection({
host : 'xxx',
user : 'xxx',
password : 'xxx',
database : 'xxx'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
} else {
console.log('mysql connection established');
}
});
module.exports = connection;
Maybe there is the mistake.
A lot of promise libraries provide a map function. Seems Q does not. No matter the the same can be accomplished with vanilla promises (and Q) anyway using the all function.
First things first. Avoid defer. It makes code more difficult to reason and maintain. There are only a few rare cases when defer is needed. The rest of the time a normal promise constructor/helper functions will work better.
Normal Promises Example
function addSomething() {
var promises = _.map(data, function(item) {
return callSomethingAsync(item);
});
return Promise.all(promises);
}
Q Promises Example
function addSomething() {
var promises = _.map(data, function(item) {
return callSomethingAsync(item);
});
return $q.all(promises);
}
Presumably callSomethingAsync returns a promise. If not use the promise constructor pattern:
function toPromise(asyncFn, args) {
return new Promise(function (resolve, reject) {
function callback(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
}
asyncFn(callback, args);
});
}
function addSomething() {
var promises = _.map(data, function(item) {
return toPromise(callSomethingAsync, item);
});
return Promise.all(promises);
}
Related
Hi i have written an export module for nodejs and use to other modules in there. Unfortunately the script don't wait for the last await
authentication.prototype.adAuth = async function(loginUser, loginPassword, User) {
var authStatus = new Boolean();
var query = User.where({
username: loginUser
}).populate("provider").populate("roles");
await query.findOne(async function(err, user) {
console.log("first await");
if (err) {
authStatus = false;
}
if (user) {
if (user.provider.authModule === "activedirectory") {
var configAD = {
url: user.provider.optionFields.adURL,
baseDN: user.provider.optionFields.adUserBaseDN,
username: user.provider.optionFields.adUserDN,
password: user.provider.optionFields.adUserPassword
};
var ad = new ActiveDirectory(configAD);
await ad.authenticate(loginUser, loginPassword, function(err, auth) {
console.log("second await");
if (err) authStatus = false;
authentication.prototype.setSession(user);
console.log(auth);
authStatus = true;
});
} else {
authStatus = false;
}
} else {
authStatus = false;
}
});
return authStatus;
};
first await
POST /login 500 314.923 ms - 2497
second await
true
I don't know why the ad.authenticate doesn't wait of the result.
Presumably, because the function you are calling does not return a Promise, it uses the older style - callbacks.
You need to turn the callback function into a Promise. Try something like this:
await new Promise((resolve, reject) => {
ad.authenticate(loginUser, loginPassword, function(err, auth) {
console.log("second await");
if (err) authStatus = false;
authentication.prototype.setSession(user);
console.log(auth);
authStatus = true;
resolve();
});
})
As a general note, there's a lot of places where authStatus = false;, this suggests that you should write this piece in a more functional style. Mixing Promises and static values has a tendency to cause bugs. But that snippet above should fix your problem.
I am developping an app using node js
in my export_db.js where I export the connection and query function
When I call the function, inside the export_db the output is really a string filled with information, but in my main.js the output is undefined, as if the function wasn't finished and the code continue to run before the results comes in.
How can I force it to wait ?
File export_db.js
var con = mysql.createConnection({
....
});
con.connect(function(err) {if (err) throw err;});
con.CustomQuery = function(SQLQuery){
..DO Stuff
return stringoutput="";
con.query(SQLQuery, function (err, result,fields) {
if (err) throw err;
var arr = result;
//Do Stuff Transform result into a String
stringoutput=result as string
});
return string output;
});
module.exports = con;
File import_db.js
var db = require('../db/export_db_connection');
//DO Stuff
Queryresult = db.CustomQuery(SQLQuery);
bot.reply(Queryresult) // Send the String to the bot to get a response message
//DO Stuffs
Since your code is fundamentally asynchronous in nature (you have to wait for it to be ready). It might be better to change your _db.js to export a factory function which returns a promise which resolves with an instance of the connection when it is available.
// _db.js
function connect() {
return new Promise(function (resolve, reject) {
var con = mysql.createConnection({
//....
});
con.CustomQuery = function (SQLQuery) {
return new Promise(function(resolve, reject){
con.query(SQLQuery, function(err, result, fields){
if(err) return reject(err)
// var str = convert result to string here
resolve(str);
})
})
};
con.connect(function (err) {
if (err) return reject(err);
resolve(con)
});
})
}
let conn = null;
function getConnection() {
if (!conn) {
conn = connect();
}
return conn;
}
module.exports = getConnection;
And then, when you want to use the connection:
var getConnection = require('/path/to/_db.js');
getConnection()
.then(function (conn) {
return conn.CustomQuery(sqlQuery)
})
.then(function(str){
// Query result is available here
console.log(str);
})
You can also do this without Promises using callbacks
// _db.js
function connect(cb) {
var con = mysql.createConnection({
//....
});
con.CustomQuery = function (SQLQuery) {
//..DO Stuff
// return stringoutput="";
};
con.connect(function (err) {
if (err) return cb(err);
cb(null, con)
});
})
}
let conn = null;
function getConnection(cb) {
if (!conn) {
return connect(function(err, con){
if(err) return cb(err);
conn = con;
cb(null, conn);
});
}
cb(null, conn);
}
module.exports = getConnection;
And then, when you want to use the connection:
var getConnection = require('/path/to/_db.js');
getConnection(function (err, conn) {
if(err){
// handle errors
}
QueryResult = conn.CustomQuery(SQLQuery);
})
I have these 2 functions
Here I get details from sales table
var getLedgerDetails = function (name, id, res) {
var response = [];
var f = '%d %b %Y';
connection.query("SELECT id,voucher_type,DATE_FORMAT(date,?) as date,amount,voucher_number FROM sales WHERE ledger_name=? and company_id=?", [f, name, id], function (err, result) {
if (err) {
console.log(err)
}
else {
if (result.length > 0) {
var r = JSON.stringify(result, null, 2);
var row = JSON.parse(r);
return row[0];
}
else {
}
}
})
};
and second is
here i want to access the getLedgerDetails Function
getDetails=function(name,id,res){
//**returns undefined**
console.log(getLedgerDetails(name,id,res));
}
but it returns me undefined..It makes the function call but returns no value
where i am wrong??
Its because your code is asynchronous, you have to return your data in a callback function which will be called only at the end.
You can try something like this :
var getLedgerDetails=function(name,id,res, callback) {
var response = [];
var f = '%d %b %Y';
connection.query("SELECT id,voucher_type,DATE_FORMAT(date,?) as date,amount,voucher_number FROM sales WHERE ledger_name=? and company_id=?", [f, name, id], function (err, result) {
if (err) {
callback(err, null);
}
else {
if (result.length > 0) {
var r = JSON.stringify(result, null, 2);
var row = JSON.parse(r);
callback(null, row[0]);
}
else {
callback(null, null);
}
}
});
};
And your getDetails function
getDetails=function(name,id,res){
getLedgerDetails(name, id, res, function(err, row) {
if (err) {
console.log(err);
}
else {
console.log(row);
}
});
};
It seems you want your function getLedgerDetails to return data whereas the anonymous function associated with your connection.query function is actually returning your data. Being the asynchronous nature of javascript
In your case, you can you can use Promises.
Well, Promises provide us with a cleaner code and handling errors with promises is very easy. Also, promises are better when comes to handling nested callbacks that is one after the another.
For Promise:
var Promise = require('promise');
var getLedgerDetails=function(name,id,res) {
return new Promise(function (resolve, reject) {
var response=[];
var f='%d %b %Y';
connection.query("SELECT id,voucher_type,DATE_FORMAT(date,?)
as date,amount,voucher_number FROM sales WHERE
ledger_name=? and
company_id=? ,[f,name,id],function(err,result){
if(err){
reject(err);//or any custom error message.
}else {
if(result.length>0){
var r=JSON.stringify(result,null,2);
var row=JSON.parse(r);
resolve(row[0]);
}else{
}
}
});
}
}
Usage:
getLedgerDetails.then(function(success){
console.log(success)// your db data.
}).catch(function(error) {
// error handle
});
It's a little complicated. Let show the code:
requestAccessToken().then(function(requestResult) { // this is the first then()
if(requestResult.ok == 1) {
return document;
}
}).then(function(document) { // this is the second then()
return db.update(updateArgu); // here, db.update will return a promise obj
}).then(function(result) { // this is the last then()
console.log('update ok');
console.log(result);
});
Since the db.update(updateArgu) will return a promise object, it can add a .then() method like db.update().then().
But I want to keep the main chain like requestAccessToken().then().then() so I returned the db.update() in the second then(). The output is:
app-0 update ok
app-0 undefined
The db.update code is:
exports.update = function(arguments) {
var name = arguments.name;
var selector = (arguments.selector) ? arguments.selector : {};
var document = (arguments.document) ? arguments.document : {};
var options = (arguments.options) ? arguments.options : {};
return Promise(resolve, reject) {
MongoClient.connect(DBURL, function(err, db) {
if(err) throw err;
db.collection(name).update(selector, document, options, function(err, result) {
db.close();
if(err) {
reject(err);
} else {
resolve(result);
}
});
});
}
}
You can see it has resolve(result), how can I transfer it to the last then()?
I'll make my comment into an answer.
Where you do:
return Promise(resolve, reject) {
...
}
it should be:
return new Promise(function(resolve, reject) {
...
});
I've read through a lot of different articles on promisejs but can't seem to get it to work for my code. I have async code that works and does what I need but it's very long and doesn't look as clean as it could with promise.
Here's the two links I've been really looking into: http://jabberwocky.eu/2013/02/15/promises-in-javascript-with-q/ and https://spring.io/understanding/javascript-promises.
mainCode.js
accountModel.findOne({username: body.username}, function(err, usernameFound) {
console.log("here");
if (err) {
console.log(err);
} else {
console.log("here1");
anotherClass.duplicateUsername(usernameFound, function(err, noerr) {
if (err) {
console.log("error");
res.status(409).send("username");
} else {
console.log("here2");
accountModel.findOne({email: body.email}, function(err, emailFound) {
if (err) {
console.log("error2");
} else {
console.log("here3");
console.log(emailFound);
}
});
}
});
}
});
// anotherclass.duplicateUsername
anotherClass.prototype.duplicateUsername = function(usernameFound, callback) {
if (usernameFound) {
callback(usernameFound);
} else {
callback();
}
}
current promise code (in mainCode.js):
var promise = userModel.findOne({
username: body.username
}).exec();
promise.then(function(usernameFound) {
console.log("usernameFound")
return userCheck.duplicateUsername(usernameFound);
}).then(function(usernameFound) {
console.log("NOERR:" + usernameFound + ":NOERR");
console.log("noerror");
return;
}, function(error) {
console.log(err);
console.log("error");
res.sendStatus(409);
return;
});
When I run my promise code, it goes to duplicateUsername, does callback() but then doesn't print anything in the promise code.
duplicationUsername needs to return a promise, otherwise the promise chaining will get the value returned from calling callback (which would be undefined).
Something like this should work:
anotherClass.prototype.duplicateUsername = function(usernameFound) {
var deferred = Q.defer();
if (usernameFound) {
deferred.resolve(usernameFound);
} else {
deferred.reject();
}
return deferred.promise;
}
So it seems like I needed to "promisify" my own functions before I could use them.
Here's how I did it with Q:
var Q = require('q');
anotherClass.prototype.duplicateUsername = function(username, callback) {
return new Promise(function(resolve, reject) {
var deferred = Q.defer();
if (usernameFound) {
deferred.reject("error);
} else {
deferred.resolve("no err: duplicate username");
}
deferred.promise.nodeify(callback);
return deferred.promise;
});
}
Here's how to do it with Bluebird:
userCheck.prototype.duplicateUsername = function(usernameFound) {
return new Promise(function(resolve, reject) {
if (usernameFound) {
reject("error");
} else {
resolve();
}
});
}
Then in my mainClass, I just used them by calling the methods and then .then(//blah)