Recursive call to a async function in nodejs - javascript

I perform an async search call. The search results are not returned in one go. I get max 200 results at once. For next set of results I have to extract bookmark from the first set of results. So the query should happen sequentially. I have written following code but its not working. The next loop (for loop) is executing without finishing the first query. I dont know how to resolve this.
var getQueryResult = function(resourcetypeStr, startTime, endTime, bookmark){
var promise = new Promise(function(resolve, reject) {
var options = {
OPTIONS
};
search(DESIGN_DOC, INDEX, options) //Async function
.then(function (data) {
resolve(data);
})
.catch(function(error){
logger.error("Error querying database " + resourcetypeStr + ": " + error);
return reject(error);
});
});
return promise;
};
var getRemainingData = function(resourcetypeStr, startTime, endTime, bookmark, number){
var promise = new Promise(function(resolve, reject){
var result;
for(i = 0; i<number; i++ ){
var data = getQueryResult(resourcetypeStr, startTime, endTime, bookmark).then (function(data){
if(result){
result = result.concat(data);
}else{
result = data;
}
if(data.row.lenth ===0){
resolve(result);
}
bookmark = data.bookmark;
return data;
});
}
});
return promise;
};
var getResources = function (resourcetypeStr, list, startTime, endTime) {
var promise = new Promise(function(resolve, reject) {
var options = {
OPTIONS
};
return search(DESIGN_DOC, INDEX, options)//Async function
.then(function (data){
if(data.total_rows > 200){
debugger;
getRemainingData(resourcetypeStr, startTime, endTime, data.bookmark, function(result){
resolve(data.concat(result));
});
}else{
resolve(data);
}
})
.catch(function(error){
console.log("reject error :"+error);
return reject(error);
});
});
return promise;
};
I also tried
function getRemainingData(resourcetypeStr, uuidQueryString, startTime, endTime, bookmark, number, callback) {
var result; // clone collection
(function getOne(resourcetypeStr, uuidQueryString, startTime, endTime, bookmark) {
console.log("getOne is called");
getRemainingData(resourcetypeStr, uuidQueryString, startTime, endTime, bookmark).then(function(data){
if(result){
result = result.concat(dataToJson(data));
}else{
result = dataToJson(data);
}
if(data.row.lenth ===0){
callback(result);
}else{
setTimeout(getOne(resourcetypeStr, uuidQueryString, startTime, endTime, data.bookmark), 0);
}
} );
})();
}
with callback but this is overflowing stack. I am not sure why as in my test case there are only 300 results so it should ideal run only once here.

Have you tried something like this?
var promise = new Promise(function(resolve, reject){
var result,
callback = function callback(data) {
if (result) {
result = result.concat(data);
} else {
result = data;
}
if (data.row.length ===0) {
resolve(result);
}
else {
getQueryResult(resourcetypeStr, startTime, endTime, bookmark).then(callback);
}
};
getQueryResult(resourcetypeStr, startTime, endTime, bookmark).then(callback);
});
return promise;
When the getQueryResult returns, if the length is 0, we're done and can resolve the outer promise, else call getQueryResult again with the same callback until the length is 0. SO instead of a for loop, have each resolving getQueryResult call the next one if needed.
If this is similar to your second try, check the values of data.row.length, since this can only go infinite if data.row.length is never 0.

Related

How to write my function with Promises

I am load HTML (external app) into an iFrame
I want to "do" something (callback) when an element becomes available in my iFrame. Here how I wrote it, and I'd like to write this with Promises instead:
function doWhenAvailable(selector, callback) {
console.warn("doWhenAvailable", selector)
if ($('#myiFrame').contents().find(selector).length) {
var elt = $('#myiFrame').contents().find(selector);
console.info("doWhenAvailable Found", elt)
callback && callback(elt);
} else {
setTimeout(function() {
doWhenAvailable(selector, callback);
}, 1000);
}
}
Actually instead of using setTimeout, I'd like to use setInterval to repeat the "find element" until it's found and resolve the "promise".
No, you would not use setInterval, you just would wrap the timeout in a promise and drop the callback:
function wait(t) {
return new Promise(function(resolve) {
setTimeout(resolve, t);
});
}
function whenAvailable(selector) {
var elt = $('#myiFrame').contents().find(selector);
if (elt.length)
return Promise.resolve(elt);
else
return wait(1000).then(function() {
return whenAvailable(selector);
});
}
Keeping your recursive style, it would have become something like that :
function doWhenAvailable(selector) {
var dfd = jQuery.Deferred();
console.warn("doWhenAvailable", selector)
if ($('#myiFrame').contents().find(selector).length) {
var elt = $('#myiFrame').contents().find(selector);
console.info("doWhenAvailable Found", elt)
return dfd.resolve(elt);
} else {
setTimeout(function() {
doWhenAvailable(selector).then(function(e) {
dfd.resolve(e);
});
}, config[env].wrapper.timeOutInMs);
}
return dfd.promise();
}
But I would have tried to avoid recursive calls here
The general idea is to return a promise instead of receiving a callback.
Example:
var xpto = function(res) {
return new Promise((resolve, reject) => {
if(res > 0) resolve('Is greater');
else reject(new Error('is lower'));
});
}
So in your case:
function doWhenAvailable(selector) {
function work(callback) {
if ($('#myiFrame').contents().find(selector).length) {
var elt = $('#myiFrame').contents().find(selector);
console.info("doWhenAvailable Found", elt)
callback(elt);
}
}
return new Promise((resolve, reject) => {
console.warn("doWhenAvailable", selector)
setInterval(() => work(resolve), 1000);
})
}
Here:
function doWhenAvailable(selector) {
return new Promise(function(resolve, reject){
console.warn("doWhenAvailable", selector)
if ($('#myiFrame').contents().find(selector).length) {
var elt = $('#myiFrame').contents().find(selector);
console.info("doWhenAvailable Found", elt)
resolve(elt);
} else {
setTimeout(function() {
doWhenAvailable(selector).then(function(data){
resolve(data);
});
}, config[env].wrapper.timeOutInMs);
}
}
}
And call your function like that:
doWhenAvailable("#elemId").then(function(elt){
//do what you want
});

Promise does not resolve node.js

I am trying to resolve some promises by the sendRequest function but it does not work.
For example, if I evoke sendRequest function 4 times, all of the times, I can see the log printed in the console and then going to resolve(data). But only 1 out of 4 times, the program reaches in to sendRequest.then().
Here is the complete code of that file.
change-name.js
var sendRequest = function(fileName){
return new Promise(function (resolve,reject) {
httpRequest(fileName).then(function (data) {
try{
if(.....){
if(.....){
var lastIndex = ....;}
if(.....){
var lastIndex = ....;}
str = fileName.substring(0, lastIndex);
if(.........){
sendRequest(str);}
else{
reject("this is the end");}
}
else{
console.log("end result" + JSON.stringify(data,null,4));
resolve(data);
//===== THIS RESOLVE DOES NOT WORK WHILE THE LOG PRINTS THE DATA =====//
}
}
catch(e){
resolve(data);
}
}).catch(function (err) {
reject(err);
});
});
};
server.js
this files calls the sendRequest function from the change-name.js and the then method is applied here for that function.
fs.readdir(path,(err,files)=>{
if(err){
console.log(err);
return;
}
for(i=0;i<files.length;i++){
sendRequest(files[i]).then(function (data) {
console.log(data + "\n");
}).catch(function(err){
console.log("end Error is " + err + "\n");
});
console.log(files);
}
});
The github link is "https://github.com/abhikulshrestha22/movierator".
Any help would be appreciated. Thanks
The problem is that one of your branches calls sendRequest again rather than resolving or rejecting, but then doesn't make any use of the promise the recursive call returns returns, so the promise you created for the outer call is never resolved. The inner one is (which is why you see the message in the console), but since nothing is using that promise, you don't get the result you expect.
There's also no need for new Promise in your code at all. httpRequest already gives you a promise, and your then handler on it creates a promise (if you return a value). So sendRequest should just return the result of calling then on httpRequest's promise, and within the then callback, either return the value to resolve the new promise with, or throw to reject. In the branch where you're calling sendRequest again, return the promise it returns; the one then creates will then resolve/reject based on that promise:
var sendRequest = function(fileName) {
return httpRequest(fileName).then(function(data) {
if (condition1) {
if (condition2) {
var lastIndex = something;
}
if (condition3) {
var lastIndex = somethingElse;
}
str = fileName.substring(0, lastIndex);
if (condition4) {
return sendRequest(str);
}
else {
throw new Error("this is the end");
}
}
else {
console.log("end result" + JSON.stringify(data, null, 4));
return data;
}
});
};
Here's a live example where httpRequest returns a random number 1-10, and if the number is less than 8, calls sendRequest again; it'll try up to three times before giving up:
function httpRequest() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve(Math.floor(Math.random() * 10) + 1);
}, 500);
});
}
var sendRequest = function(fileName, retries) {
if (typeof retries !== "number") {
retries = 3;
}
return httpRequest(fileName).then(function(data) {
console.log("`then` got: " + data);
if (data > 7) {
console.log("It's > 7, yay! We're done");
return data;
}
console.log("It's <= 7g");
if (--retries === 0) {
console.log("Out of retries, rejecting");
throw new Error("out of retries");
}
console.log("retrying (" + retries + ")");
return sendRequest(fileName, retries);
});
};
document.getElementById("btn").addEventListener("click", function() {
var btn = this;
btn.disabled = true;
console.log("Sending request");
sendRequest("foo")
.then(function(data) {
console.log("Request complete: " + data);
})
.catch(function(err) {
console.log("Request failed: " + err);
// Because we don't throw here, it converts the rejection into a resolution...
})
.then(function() {
// ...which makes this kind of like a "finally"
btn.disabled = false;
});
}, false);
<input type="button" id="btn" value="Start">

Why isn't my promise getting resolved?

I have two functions - A helper function for downloading files which is as follows
var downloadHelper = function(url, saveDir) {
var deferred = Q.defer();
setTimeout(function() {
deferred.resolve("success");
}, 2000);
return deferred.promise;
}
Now I have a list of files to be downloaded in parallel. I have the logic for that function as follows:
var downloadAll = function() {
var fileDownloadList = []
for(var key in config.files) {
var deferred = Q.defer();
var saveLocation = __base + config.localDir
downloadHelper(
config.files[key],
saveLocation
).then(function() {
deferred.resolve("downloaded: " + fileUrl);
}).catch(function(err) {
deferred.reject(err);
});
fileDownloadList.push(deferred.promise);
}
Q.all(fileDownloadList).done(function() {
console.log("All downloaded");
},function(err) {
console.log(err);
});
setTimeout(function() {
console.log(fileDownloadList);
}, 10000);
}
The done is never getting called!
For debugging purposes, I added a setTimeout that will be called after 10 seconds and what I see is that out of 2 files, the second promise is resolved and the first one is still in pending state.
Any ideas?
Thanks in advance
One way to make your code work
for(var key in config.files) {
(function() {
var deferred = Q.defer();
var saveLocation = __base + config.localDir
downloadHelper(
config.files[key],
saveLocation
).then(function() {
deferred.resolve("downloaded: " + fileUrl);
}).catch(function(err) {
deferred.reject(err);
});
fileDownloadList.push(deferred.promise);
}());
}
But since downloadhelper returns a promise, no need to create yet another one
for (var key in config.files) {
var saveLocation = __base + config.localDir
fileDownloadList.push(downloadHelper(
config.files[key],
saveLocation
).then(function () {
return("downloaded: " + fileUrl);
}));
}
You'll see I removed
.catch(function(err) {
deferred.reject(err);
})
That's redundant, it's the same as not having the catch at all

stoping async tasks running in parallel

I'm trying to run through (using foreach) an array of objects and then for each I'd like to call a function that uses request to get a file and then unzips it with zlib, but one at a time, given the nature of node this is currently done asynchronously.
I'd like it to be done something like this...
- foreach - first object
- call function for first object
- when function has completed
- go to the next object in the array
I have tried using the SYNC module to try and solve this but with no luck.
Any ideas on how I can achieve this?
// the function i am trying to run for each in sync
var downloadUnzipFile = function(mID) {
try {
// Read File
console.log("Started download/unzip of merchant: " + mID + " # " + new Date().format('H:i:s').toString());
request(linkConst(mID))
// Un-Gzip
.pipe(zlib.createGunzip())
// Write File
.pipe(fs.createWriteStream(fileName(mID)))
.on('error', function(err) {
console.error(err);
})
.on('finish', function() {
console.log("CSV created: " + fileName(mID));
console.log("Completed merchant: " + mID + " # " + new Date().format('H:i:s').toString());
//console.log("Parsing CSV...");
//csvReader(fileName);
});
} catch (e) {
console.error(e);
}
}
module.exports = function(sMerchants) {
var oMerchants = JSON.parse(JSON.stringify(sMerchants));
sync(function() {
oMerchants.forEach(function eachMerchant(merchant) {
downloadUnzipFile(merchant.merchant_aw_id);
})
})
};
var promiseQueue = (function() {
'use strict';
var promiseQueue = function() {
var queue = [Promise.resolve(true)];
var add = function(cb) {
var args = Array.prototype.slice.call(arguments);
args.shift();
queue.unshift(new Promise(function(resolve) {
queue[0].then(function() {
resolve(cb.apply(null, args));
queue.pop();
});
}));
};
return {
add: add
}
}
return promiseQueue;
}());
usage EXAMPLE:
This is the asynch function that will be called
var theFun = function (time, n) { // use whatever arguments you like that will be called with your function
return new Promise(function(resolve) {
//asynch function goes here INSTEAD of the setTimeout and it's contents, I repeat, INSTEAD of the setTimeout
setTimeout(function() { // this is for demonstrating ONLY
console.log('resolving', n, time); // this is for demonstrating ONLY
resolve(time); // this is for demonstrating ONLY
}, time); // this is for demonstrating ONLY
// remember to resolve("someValueNotImportantAsItIsntUsedAnywhere") on completion of your asynch function
});
}
This is how the items get added to the queue - I did it this way because of MY use case
var pq = promiseQueue();
for(var i = 0; i < 5; i++ ) {
var r = 1000 - i * 150;
console.log('adding ', i, r);
pq.add(theFun, r, i);
}
Hope you find this of some use
First, your function needs to take a callback so it can communicate when it has finished:
var downloadUnzipFile = function(mID, next) {
try {
// Read File
console.log("Started download/unzip of merchant: " + mID + " # " + new Date().format('H:i:s').toString());
request(linkConst(mID))
// Un-Gzip
.pipe(zlib.createGunzip())
// Write File
.pipe(fs.createWriteStream(fileName(mID)))
.on('error', function(err) {
console.error(err);
})
.on('finish', function() {
console.log("CSV created: " + fileName(mID));
console.log("Completed merchant: " + mID + " # " + new Date().format('H:i:s').toString());
//console.log("Parsing CSV...");
//csvReader(fileName);
next();
});
} catch (e) {
console.error(e);
next();
}
}
Then, we need to recursively call each one when the previous has finished:
module.exports = function(sMerchants, next) {
var oMerchants = JSON.parse(JSON.stringify(sMerchants));
var i = 0;
var run = function() {
if(i < oMerchants.length)
downloadUnzipFile(i++, run);
else
next();
};
};
Note that I also added a callback to the exported function, so it can communicate when it is finished. If this is unnecessary, you can drop it.
This may work for you, uses Promise. Need to add resolve and reject callbacks to your downloadUnzipFile-
var exports = (function () {
'use strict';
var pre = document.getElementById('out');
function log(str) {
pre.appendChild(document.createTextNode(str + '\n'));
}
function downloadUnzipFile(id, resolve, reject) {
log('Start: ' + id);
try {
setTimeout(function () {
resolve(id);
}, 3000);
} catch (e) {
reject(e);
}
}
function done(id) {
log('Done: ' + id);
}
function error(e) {
log(e.message);
}
function getPromise(mID) {
return new Promise(function (resolve, reject) {
downloadUnzipFile(mID, resolve, reject);
});
}
return function (sMerchants) {
JSON.parse(sMerchants).reduce(function (next, mID) {
if (!next) {
next = getPromise(mID);
} else {
next = next.then(function (id) {
done(id);
return getPromise(mID);
}, error);
}
return next;
}, null).then(done, error);
};
}());
exports(JSON.stringify([1, 2, 3, 4, 5]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/json2/20150503/json2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.7/es5-shim.min.js"></script>
<script src="https://rawgit.com/jakearchibald/es6-promise/master/dist/es6-promise.min.js"></script>
<pre id="out"></pre>
I added the browser shims to support older browsers that may be viewing this, you shouldn't need them on node.js but you may need to require a Promise shim if you are using an old node.js.

setTimeout not working in script for retrying database connections

I'm working on some code where I want to try to make a database connection and retry a limited number times, increasing the time between each attempt by double.
I have a semi-working version. The only issue is that I don't believe the setTimeout timing is working as each consecutive retry appears to execute immediately. Here's the script on jsfiddle.
var NUM_RETRIES = 10;
var TIMEOUT = 1;
var initConnection = function(callback) {
var result = Math.floor((Math.random()*5)+1);
console.log(result);
if(result > 1) {
return callback('error: trouble connecting.');
}
return callback(null);
};
var tryConnection = function(callback) {
setTimeout(initConnection(function(err) {
if(err) {
TIMEOUT *= 2000;
NUM_RETRIES -= 1;
if(NUM_RETRIES) {
return tryConnection.call(this, callback);
}
return callback('error: gave up.');
}
return callback();
}), 20000);
};
var run = function() {
tryConnection(function(err) {
if(err) {
return console.log(err);
}
console.log('connected!');
});
};
run();
Try this:
// initConnection should be called a max of 50 times
// timeout timer in ms
var NUM_RETRIES = 50;
var TIMEOUT = 200;
var initConnection = function (callback) {
var result = Math.floor((Math.random() * 5) + 1);
console.log(result);
if (result > 1) {
return callback('error: trouble connecting.');
}
return callback(null);
};
var tryConnection = function (callback) {
setTimeout(function () {
console.log('timeout:' + TIMEOUT + 'ms');
initConnection(function (err) {
if (err) {
TIMEOUT = TIMEOUT * 2;
NUM_RETRIES -= 1;
if (NUM_RETRIES) {
return tryConnection.call(this, callback);
}
return callback('error: gave up.');
}
return callback();
})
}, TIMEOUT);
};
var run = function () {
tryConnection(function (err) {
if (err) {
return console.log(err);
}
console.log('connected!');
});
};
run();
In setTimeout, we need to pass a function which will call after the time delay. But you are calling that function/method in timeout, instead of passing it, which results in unexpected behaviour.
Example:
var callMe = functiion() {}
setTimeout(callMe(), 20000); // call immediately
setTimeout(callMe, 20000); // call after 20000ms delay
JSFiddle Demo

Categories