I've been struggling to figure out how to do this for the past two hours. But I can't seem to get the Promise to wait for my searchFacesFunc to complete before it solves. What is the correct way to do so?
async function searchFacesFunc(faceId){
var searchFacesParams = {
CollectionId: "my-collection",
FaceId: faceId,
FaceMatchThreshold: 80,
MaxFaces: 10
};
await rekognition.searchFaces(searchFacesParams, function(err, data) {
if(err){
throw err;
}else{
var matching_percent = data.FaceMatches[0].Similarity;
console.log('Matching Percent: ' + matching_percent);
}
});
}
return new Promise((resolve, reject) => {
rekognition.indexFaces(indexParams, function(err, data) {
if(err){
throw err;
}else{
const faceRecords = data.FaceRecords;
for(let i = 0; i < faceRecords.length; i++){
var faceId = faceRecords[i].Face.FaceId;
console.log('FaceId: ' + faceId);
searchFacesFunc(faceId); //The promise is finished before these multiple functions finish
}
resolve(null, 'success');
}
});
});
If the rekognition.indexFaces function accepts an asynchronous callback, you can solve this issue easily:
return rekognition.indexFaces(indexParams, async (err, data) => {
if (err) {
throw err;
} else {
const faceRecords = data.FaceRecords;
for (let i = 0; i < faceRecords.length; i++) {
var faceId = faceRecords[i].Face.FaceId;
console.log("FaceId: " + faceId);
await searchFacesFunc(faceId); // Await the promise
}
return "success";
}
});
};
However, if this is not the case, you can still solve this the following way:
Use util.promisify to "promisify" the rekognition.indexFaces function
Construct a recursive callback function that only resolves the original promise you constructed when it executed faceRecords.length times.
Related
js
I just create a 2 function to send the response var = y but unfortunately res.end() work before then my y++ works..
This code work well with for loop without mine query
but when i used a query it working stop and it gives res like below.
Before promise call.
Loop completed.
Promise resolved: 0
Next step.
13289,
2170,
2165,
2146,
2115,
2138,
2143,
2112,
1964,
2685,
6945,
2687,
2688,
2902,
2935,
2941,
3094,
3095,
3164,
3096,
var http = require('http');
var url = require('url');
var fs = require('fs');
var mysql = require('mysql');
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
var end = '';
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "justcode"
});
//1. Create a new function that returns a promise
function firstFunction() {
return new Promise((resolve, reject) => {
let arr = [];
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM wp_posts WHERE `post_type` = 'post'", async function (err, result, fields) {
if (err) throw err;
result.forEach(element => {
arr.push(element['ID']);
});
resolve(arr)
});
});
})
}
//2. Create an async function
async function secondFunction() {
//3. Await for the first function to complete
const result = await firstFunction()
res.end(JSON.stringify(result));
};
secondFunction()
}).listen(8080);
This is the working code::::
How about this...
//1. Create a new function that returns a promise
function firstFunction() {
return new Promise((resolve, reject) => {
let y = 0
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM wp_posts WHERE `post_type` = 'post'", async function (err, result, fields) {
if (err) throw err;
result.forEach(element => {
console.log(element['ID']);
y++;
});
// get the results from post table
console.log('Loop completed.')
resolve(y); // <---- Resolve the promise here
});
});
// for (i = 0; i < 100; i++) {
// y++
// }
// console.log('Loop completed.')
// resolve(y) // <--- Moving this inside con.query callback
})
}
//2. Create an async function,
async function secondFunction() {
console.log('Before promise call.')
//3. Await for the first function to complete
const result = await firstFunction()
console.log('Promise resolved: ' + result)
console.log('Next step.')
res.end();
};
secondFunction()
Problem in The Original Question
con.query is an asynchronous execution. It accepts a callback function which then it invokes once the operation has completed. javascript has something called runToCompletion in which the execution of a function continues until the last statement.
When secondFunction is invoked, it further invokes(with an await) firstFunction which returns a Promise and the execution of the promise starts, but the caller secondFunction is waiting for the promise to finish.
Inside the execution of the Promise of firstFunction, there is a call to con.query, which is an asynchronous call, but isn't being waited for. Hence, after invoking the call to con.query, the javascript execution continues further... only to encounter a resolve after a couple of statements. A resolve finishes the execution of the Promise of firstFunction and control passes back to secondFunction and then execution continues from there on. Now, since the call to con.query will finish after sometime, and when it does, the callback function will be executed. And so we are seeing the behavior as problematic. But it's all happening as per the semantics of javascript execution.
Assume you are using mysql, since it does not provide a promise API out of the box, you may need to promisify the connect() call and query() call yourself and await for them.
async function firstFunction() {
let y = 0;
await new Promise((resolve,reject)=>{
con.connect(function (err) {
if (err) reject(err);
resolve();
});
});
let result = await new Promise((resolve,reject)=>{
con.query("SELECT * FROM wp_posts WHERE `post_type` = 'post'", (err, result, fields) => {
if (err) reject(err);
resolve(result);
// get the results from post table
});
});
result.forEach(element => {
console.log(element['ID']);
y++;
});
// for (i = 0; i < 100; i++) {
// y++
// }
console.log('Loop completed.')
return y;
}
Alternatively, you may try using wrapper library like mysql-promise and promise-mysql
I'm trying to use Promise.all function but actually as I start nodeJS and I discover asynchronous technology I don't know where is my problem in my code...
Basically I would like to use Promise.all to make my own callback in a function then In my loop For I create several Promise and if I can save my data then I'll resolve my current Promise.
But apparently my promise.all is executed immediately and it don't wait for my others Promise..
See below ..
function persistMAP(jsonData,callback){
//Deck persistance
const promises =[];
for(var i=0; i<1; i++){
(function(i) {
var rowData = new DeckDatabase({
_id: new mongoose.Types.ObjectId(),
DeckNumber: Number(jsonData.Deck[i].DeckNumber),
x: Number(jsonData.Deck[i].x),
y: Number(jsonData.Deck[i].y),
});
rowData.save(function (err) {
if (err) return console.log(err);
for(var index=0; j=jsonData.Units.length,index<j; index++){
(function(index) {
promises.push(
new Promise(function(resolve,reject){
var unit = new MapDatabase({
UnitID:jsonData.Units[index].UnitID,
TypeID: Number(jsonData.Units[index].TypeID),
x: Number(jsonData.Units[index].x),
y: Number(jsonData.Units[index].y),
_id: mongoose.Types.ObjectId(((jsonData.Units[index].Code).toLowerCase()) + 'dd40c86762e0fb12000003'), // mongoose.Types.ObjectId(jsonData.Units[i].Code + 'dd40c86762e0fb12000003')
MainClass: jsonData.Units[index].MainClass,
Orientation: jsonData.Units[index].Orientation,
Postion: jsonData.Units[index].Postion,
Deck : String(rowData._id)
});
unit.save(function (err) {
if (err) {
reject();
throw err
}
else{
console.log('save');
resolve();
}
});
})
);
})(index);
}
});
})(i);
}
Promise.all(promises)
.then(()=>{
console.log('start find');
callback();
})
};
and here is where I call my function
else{
var jobj = JSON.parse(response.body);
console.log("persist begin");
persistMAP(jobj,function(){
console.log('retrieve Done');
MapDatabase.find()
.populate('Deck')
.exec(function(err, finalData){
console.log('send');
res.send(finalData);
})
});
}
So why It doesn't wait ? :(
You do an async operation in your for:
for(var i=0; i<1; i++){
....
rowData.save(function (err) {
So your promises array isn't filled with all the promise.
To loop async use async module :
var async = require("async");
var models = []; // my models array
// async loop, with 10 in same time, e is the element, i the index in models
async.eachOfLimit(models, 10, function(e, i, cb){
anAsyncFunction(function(err){
return cb(err);
});
}, function(err, result{
// all done
});
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
});
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);
}
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) {
...
});