I have a asynchronous code which I want to run synchronously in one of my node js script, But this doesn't wait for the code block to complete and resolves the empty object -
new Promise((resolve, reject) => {
if (object.email !== undefined) {
for (let i = 0; i <= object.email.length; i++) {
let emailObject = object.email[i]
if (emailObject !== undefined) {
this.isEmailUnsubscribed(emailObject, options).then(result => {
console.log('>> isEmailUnsubscribed result in send email notification: ' + result)
if (!result) {
emailObjects.push(emailObject.EmailID)
}
})
}
}
console.log('emailObjects')
console.log(emailObjects)
resolve(emailObjects)
}
}).then(emailObjects => {
object.email = emailObjects
console.log('Email Objects from rules.evaluate')
console.log(emailObjects) // At this point my object is always empty.
this.sendEmailToSelectedUsers(object, options)
})
This is because your loop is generating new promises that are resolved asycnoursly, use Promise.all when you need to run multiple promises:
For example:
if (object.email !== undefined) {
return Promise.all(object.email.map( emailObject => {
if(emailObject){
return this.isEmailUnsubscribed(emailObject, options)
}else{
return Promise.resolve()
}
} ))
.then(emailObjects => {
object.email = emailObjects
console.log('Email Objects from rules.evaluate')
console.log(emailObjects) // At this point my object is always empty.
this.sendEmailToSelectedUsers(object, options)
})
}
Related
I have my function whose job is to go over a number of files (that use the values from the array as building blocks for file names) and download them using a reduce. It's more of a hack as of now but the Promise logic should work. Except it doesn.t
Here's my code:
function import_demo_files(data) {
/**
* Make a local copy of the passed data.
*/
let request_data = $.extend({}, data);
const get_number_of_files_1 = Promise.resolve({
'data' : {
'number_of_files' : 2
}
});
return new Promise((resolve, reject) => {
let import_files = get_number_of_files_1.then(function(response) {
new Array(response.data.number_of_files).fill(request_data.step_name).reduce((previous_promise, next_step_identifier) => {
let file_counter = 1;
return previous_promise.then((response) => {
if( response !== undefined ) {
if('finished_import' in response.data && response.data.finished_import === true || response.success === false) {
return import_files;
}
}
const recursively_install_step_file = () => import_demo_file({
demo_handle: request_data.demo_handle,
'step_name': request_data.step_name,
'file_counter': file_counter
}).call().then(function(response) {
file_counter++;
if('file_counter' in response.data && 'needs_resume' in response.data) {
if(response.data.needs_resume === true) {
file_counter = response.data.file_counter;
}
}
return response.data.keep_importing_more_files === true ? recursively_install_step_file() : response
});
return recursively_install_step_file();
}).catch(function(error) {
reject(error);
});
}, Promise.resolve())
}).catch(function(error) {
reject(error);
});
resolve(import_files);
});
}
Now, when I do:
const import_call = import_demo_files({ 'demo_handle' : 'demo-2', 'step_name' : 'post' });
console.log(import_call);
The console.log gives me back that import_call is, in fact a promise and it's resolved. I very much like the way return allows me to bail out of a promise-chain, but I have no idea how to properly resolve my promise chain in there, so clearly, it's marked as resolved when it isn't.
I would like to do import_call.then(... but that doesn't work as of now, it executes this code in here before it's actually done because of the improper handling in import_demo_files.
An asynchronous recursion inside a reduction isn't the simplest of things to cut your teeth on, and it's not immediately obvious why you would want to given that each iteration of the recursion is identical to every other iteration.
The reduce/recurse pattern is simpler to understand with the following pulled out, as outer members :
1. the `recursively_install_step_file()` function
1. the `new Array(...).fill(...)`, as `starterArray`
1. the object passed repeatedly to `import_demo_file()`, as `importOptions`)
This approach obviates the need for the variable file_counter, since importOptions.file_counter can be updated directly.
function import_demo_files(data) {
// outer members
let request_data = $.extend({}, data);
const importOptions = {
'demo_handle': request_data.demo_handle,
'step_name': request_data.step_name,
'file_counter': 1
};
const starterArray = new Array(2).fill(request_data.step_name);
function recursively_install_step_file() {
return import_demo_file(importOptions).then((res) => {
if('file_counter' in res.data && 'needs_resume' in res.data && res.data.needs_resume) {
importOptions.file_counter = res.data.file_counter; // should = be += ?
} else {
importOptions.file_counter++;
}
return res.data.keep_importing_more_files ? recursively_install_step_file() : res;
});
}
// the reduce/recurse pattern
return starterArray.reduce((previous_promise, next_step_identifier) => { // next_step_identifier is not used?
let importOptions.file_counter = 1; // reset to 1 at each stage of the reduction?
return previous_promise.then(response => {
if(response && ('finished_import' in response.data && response.data.finished_import || !response.success)) {
return response;
} else {
return recursively_install_step_file(); // execution will drop through to here on first iteration of the reduction
}
});
}, Promise.resolve());
}
May not be 100% correct but the overall pattern should be about right. Be prepared to work on it some.
I'm working on a project in Node.js that uses user certificates. I need to generate them synchronously, in a blocking manner, but the library I use (pem) has only asynchronous functions (callbacks). I tried multiple ways to tackle the problem, but none of my tries have been successful. My code looks like this:
function KeyObject(CN, serverKey, days = 365) { // key object
if (typeof CN !== 'string' ||
typeof days !== 'number' ||
typeof serverKey !== 'object') {
throw TypeError;
}
this.CN = CN;
this.days = days;
const _this = this;
async function generatePrivate() {
var p = new Promise((resolve, reject) => {
pem.createPrivateKey((err, obj) => {
if (err) reject(err);
_this.private = obj.key;
resolve();
});
});
await p;
}
async function generateCert(serviceKey) {
if (typeof serviceKey !== 'object') {
throw TypeError;
}
var p = new Promise((resolve, reject) => {
pem.createCertificate({
commonName: _this.CN,
days: _this.days,
serviceKey: serviceKey.private
}, (err, obj) => {
if (err) reject(err);
_this.cert = obj.certificate;
resolve();
});
});
await p;
}
// init the keys
generatePrivate();
generateCert(serverKey);
}
This code goes straight through and doesn't wait for the functions to complete. What should I do?
Thanks in advance.
You should just return the Promise from each of those functions - no need to await or make them async. You also can't have an async constructor, perhaps adding an async factory would work.
I am using loadash map function to do some structuring of data. The data returns a set of ids for which i need to query the db and add the result to the map and then return but the data is returned later and my function returns first. How to make the return of map wait and then return in function.
let result = _.chain(value)
.groupBy('pubId')
.pairs()
.map(function(currentItem) {
let item = _.object(_.zip(['ID', 'targetting'], currentItem));
propsToBeDeleted.forEach(function(prop) {
item.targetting.forEach(d => {
item.publishername = d.RTB.name;
item.type = d.type;
delete d[prop];
});
});
item.targetting.map(d => {
if (d.type == 15) {
d.name = Object.keys(JSON.parse(d.value));
d.value = Object.values(JSON.parse(d.value))[0];
item.dspIds = [];
}
if (d.type == 16) {
let value = JSON.parse(d.value);
d.name = Object.keys(JSON.parse(d.value)).filter(d => d != 'profileId');
d.value = value;
item.dspIds = d.value.profileId;
dspAccount.find({where: {id: 139}}, (err, data) => {
// async call wait here to get data and attach to item
item.x = data ;
});
delete d.value.profileId;
d.value = JSON.stringify(d.value);
}
});
return item;
})
.value();
I also tried using promises
promises.push(new Promise((resolve, reject) => {
if (data.id) {
XX.upsertWithWhere({id: data.id}, payload, (err, data) => {
if (err) {
return reject(err);
}
return resolve(data);
});
}
}));
});
Promise.all(promises).then((data) => {
callback(null, data);
}, err => {
callback(err);
});
Update
i have already listed the promise .all method that doesnot work . So it cannot be called duplicate
Things are slightly tricky because you have an outer .map() and an inner .map(), containing an asynchronous element.
The asynchronous element effectively makes the whole thing asynchronous, requiring that :
the array of Promises generated by the inner .map() is aggregated with Promise.all(), and the aggregate Promise returned to the outer .map().
the array of Promises generated by the outer .map() is aggregated with Promise.all().
the results (an array of items) is available only via a chained .then().
First, to make the main code block simpler and easier to read, create a promisified version of dspAccount.findAsync().
dspAccount.findAsync = function(query) {
return new Promise(function(resolve, reject) {
dspAccount.find(query, (err, data) => {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
Then the main code block can be written as follows :
let outerPromises = _.chain(value)
.groupBy('pubId')
.pairs()
.map(function(currentItem) {
let item = _.object(_.zip(['ID', 'targetting'], currentItem));
item.targetting.forEach(d => {
item.publishername = d.RTB.name;
item.type = d.type;
propsToBeDeleted.forEach(function(prop) {
delete d[prop];
});
});
let innerPromises = item.targetting.map(d => {
let value = JSON.parse(d.value);
if (d.type == 15) { // synchronous - `innerPromises` receives a null
item.dspIds = []; //
d.name = Object.keys(value);
d.value = Object.values(value)[0];
return null;
}
if (d.type == 16) { // asynchronous - `innerPromises` receives a Promise
return dspAccount.findAsync({where: {id: 139}})
.then(data => {
d.name = Object.keys(value).filter(d => d != 'profileId');
item.dspIds = value.profileId;
delete value.profileId;
item.x = data;
d.value = JSON.stringify(value);
});
}
});
return Promise.all(innerPromises).then(() => item); // aggregate inner promises and return
})
.value(); // returns the outerPromises array
Promise.all(outerPromises) // aggregate outer promises
.then(items => {
// do stuff with items here
})
.catch(error => {
// something went wrong
// log/display error
// take remedial action as required
});
I expect there's still some work to do. It doesn't look right that item should be mutated in the item.targetting.map() loop. The last element of item.targetting to deliver its result (synch or asynch) will determine the composition of item. That may not be what you want. If it is what you want, then more control will need to be exercised to ensure that the last result delivered is the result arising from the final element of item.targetting.
I'm using for loop to iterate over an array of elements and to call the same function with different parameters inside the for loop. Here is my code:
exports.listTopSongs = function(query) {
return new Promise(function(resolve, reject) {
var str = query.split(","), category,
for(var i=0; i<str.length; i++) {
sampleFn(str[i], 'sample', resolve, reject);
}
});
};
function sampleFn(lang, cat, resolve, reject) {
client.on("error", function (err) {
console.log(err);
var err = new Error('Exception in redis client connection')
reject(err);
});
client.keys(lang, function (err, keys){
if (err) return console.log(err);
if(keys.length != 0) {
client.hgetall(keys, function (error, value) {
var objects = Object.keys(value);
result['title'] = lang;
result[cat] = [];
var x =0;
for(x; x<objects.length; x++) {
var val = objects[x];
User.findAll({attributes: ['X', 'Y', 'Z'],
where: {
A: val
}
}).then(data => {
if(data != null) {
//some actions with data and stored it seperately in a Json array
if(result[cat].length == objects.length) {
resolve(result);
}
} else {
console.log(""+cat+" is not avilable for this value "+data.dataValues['X']);
}
});
}
});
});
}
Here it won't wait for completion of first iteration. It just run asyncronously before completing first iteration function. I need to return the result as result:[{ 1, 2}, {3,4}]. but it runs seamlessly and returns empty or only one object before completing all. How to resolve it.
I used node-async-loop. But it uses next and i can't able to send my parameteres while using that package. Please help me
Async provides control flow methods allowing to do so.
Using async.each:
async.each(openFiles, function(file, callback) {
// Perform operation on file here.
console.log('Processing file ' + file);
if( file.length > 32 ) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err) {
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
If you don't want to use a library, you can code it yourself. It would also be very instructive. I took your issue and coded a dummy async loop :
function listTopSongs(query) {
return new Promise(async(resolve, reject) => { //add async here in order to do asynchronous calls
const str = query.split(",") //str is const, and the other variable was not used anyway
for( let i = 0;i < str.length; i++) {
const planet = await sampleFn(str[i], 'sample', resolve, reject)
console.log(planet)
}
});
};
function sampleFn(a, b, c, d) {
return fetch(`https://swapi.co/api/planets/${a}/`)
.then(r => r.json())
.then(rjson => (a + " : " + rjson.name))
}
listTopSongs("1,2,3,4,5,6,7,8,9")
I used some dummy star wars API to fake a long promise but it should work with your sampleFn. Be careful, it is very, very slow if you have network call like the one in the example.
EDIT: I ran your code and I noticed there are a few mistakes: there is no resolve in your promise so it's not a thenable (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve see thenable )
Here is a fully working code. The nice part : no library needed, no dependencies.
//for node.js, use node-fetch :
//const fetch = require("node-fetch")
function listTopSongs(query) {
return new Promise(async(resolve, reject) => { //add async here in order to do asynchronous calls
const str = query.split(",") //str is const, and the other variable was not used anyway
const planets = []
for (let i = 0; i < str.length; i++) {
const planet = await sampleFn(i + 1, str[i], resolve, reject)
planets[i] = planet
console.log(planet)
}
resolve(planets)
});
};
function sampleFn(a, b, c, d) {
return fetch(`https://swapi.co/api/planets/${a}/`)
.then(r => r.json())
.then(rjson => (a + b + " : " + rjson.name))
}
listTopSongs("a,b,c,d").then(planets => console.log(planets))
Since you are using promise, you can do something like this
exports.listTopSongs = function(query) {
return Promise.resolve(true).then(function(){
var str = query.split(",");
var promises = str.map(function(s){
return sampleFn(str[i], 'sample');
});
return Promise.all(promises);
}).then(function(results){
//whatever you want to do with the result
});
};
For this to work you have to change your sampleFn to not to depend on external resolve and reject functions. I don't see a reason using external resolve and reject. why just not use Promise.Resolve, Promise.Reject;
I need to iterate through an array and save every object to the Database.
At the end I need a callback with an array of all of the saved and failed object.
Below is the code I have:
exports.addList = (app, body, callback) => {
var savedObjects = []
var failedObjects = []
body.forEach((element, index) => {
body[index] = _.pick(element, 'userAId','userBId')
db.List.create(element).then((list) => {
savedObjects.push(element)
if (index == body.length - 1) {
callback(savedObjects, failedObjects)
}
}).catch((error) => {
if (error.name === "SequelizeUniqueConstraintError") {
failedObjects.push(element)
if (index == body.length - 1) {
callback(savedObjects, failedObjects)
}
})
})
}
The code above works. Is there a way better to accomplish this?
I would recommend the following approach using Promise.all() to run the db.List.create() in parallel as it will return a Promise. By mapping the body array elements to Promises you can achieve better performance as they will run in parallel (and not have to track the complete count).
exports.addList = (app, body, callback) => {
var savedObjects = [];
var failedObjects = [];
Promise.all(
// map the array to return Promises
body.map(element => {
const list = _.pick(element, 'userAId','userBId');
return db.List.create(list)
.then(() => savedObjects.push(list))
.catch((error) => {
if (error.name === 'SequelizeUniqueConstraintError') {
failedObjects.push(list)
}
})
})
)
// when all Promises have resolved return the callback
.then(() => callback(savedObjects, failedObjects));
}
In your example your complete callback will always fire after the first promise is complete. This is because the create function is asynchronous while the surrounding loop is not, therefore the loop will have already completed by the time your first callback is triggered.
In your scenario this means element and index will always be the last in the loop. One way around this would be to move your promise chain into it's own function.
In this example I've used an extra flag to track the number of completed promises to trigger your complete method.
exports.addList = (app, body, callback) => {
var savedObjects = []
var failedObjects = []
var complete = 0;
function createElement(element){
db.List.create(element).then((list) => {
savedObjects.push(element)
}).catch((error) => {
if (error.name === "SequelizeUniqueConstraintError") {
failedObjects.push(element)
}
}).finally(() => {
complete++;
if(complete == body.length) {
callback(savedObjects, failedObjects)
}
});
}
body.forEach((element, index) => {
body[index] = _.pick(element, 'userAId','userBId');
createElement(element);
})
}