Javascript recursive function with defer not returning - javascript

I got this recursive function. I can see it loop through when data return is null but it did not return the promise when data is not null after done the recursive task. Seem like when finish doing the recursive task, the promise is lost somewhere. Would anyone point out what did I do wrong here?
var callrq1 = function(globalsystemid, globalgraphid, start, end, lastcheck) {
var datetimeformat = "YYYY-MM-DD HH:mm:ss";
var d1 = new $.Deferred();
var request1 = "../system/" + globalsystemid + "/highcharts.xml?type=" + globalgraphid + "&start=" + start + "&end=" + end;
var requeststring1 = makejson(request1); //this makejson function is an ajax get and return promise
requeststring1.done(function(data) {
if (data != null) {
d1.resolve(data);
} else {
var theend = moment(lastcheck).format(datetimeformat);
var newstart = moment(end).format(datetimeformat);
var newend = moment(end).add(1, 'weeks').format(datetimeformat);
if (newend <= theend) {
//recursive callrq1
callrq1(globalsystemid, globalgraphid, newstart, newend, theend);
} else {
d1.resolve(null);
}
}
});
return d1.promise();
}
callrq1(globalsystemid, globalgraphid, starttimeobj.start, starttimeobj.end, endtimeobj.start).then(function(data) {
console.log(data);
});

You missed resolving your deferred in the case of the recursive call. However, you shouldn't be using a deferred for this in the first place! Just chain a then callback and return the result promise from your function. You can even return promises from the callback, which we use for the recursive case:
function callrq1(globalsystemid, globalgraphid, start, end, lastcheck) {
var datetimeformat = "YYYY-MM-DD HH:mm:ss";
var request1 = "../system/" + globalsystemid + "/highcharts.xml?type=" + globalgraphid + "&start=" + start + "&end=" + end;
var requeststring1 = makejson(request1); //this makejson function is an ajax get and return promise
return requeststring1.then(function(data) {
//^^^^^^ ^^^^
if (data != null) {
return data;
// ^^^^^^
} else {
var theend = moment(lastcheck).format(datetimeformat);
var newstart = moment(end).format(datetimeformat);
var newend = moment(end).add(1, 'weeks').format(datetimeformat);
if (newend <= theend) {
return callrq1(globalsystemid, globalgraphid, newstart, newend, theend);
// ^^^^^^
} else {
return null;
// ^^^^^^
}
}
});
}

You are not resolving the deferred in the case of recursion
var callrq1 = function (globalsystemid, globalgraphid, start, end, lastcheck) {
var datetimeformat = "YYYY-MM-DD HH:mm:ss";
var d1 = new $.Deferred();
var request1 = "../system/" + globalsystemid + "/highcharts.xml?type=" + globalgraphid + "&start=" + start + "&end=" + end;
var requeststring1 = makejson(request1); //this makejson function is an ajax get and return promise
requeststring1.done(function (data) {
if (data != null) {
d1.resolve(data);
} else {
var theend = moment(lastcheck).format(datetimeformat);
var newstart = moment(end).format(datetimeformat);
var newend = moment(end).add(1, 'weeks').format(datetimeformat);
if (newend <= theend) {
//recursive callrq1
callrq1(globalsystemid, globalgraphid, newstart, newend, theend).done(function(data){
d1.resolve(data);//pass any data required
});
} else {
d1.resolve(null);
}
}
});
return d1.promise();
}
callrq1(globalsystemid, globalgraphid, starttimeobj.start, starttimeobj.end, endtimeobj.start).then(function (data) {
console.log(data);
});

Related

Async call in javascript For Loop not working

I have a callback function inside a loop here for (var res in results) {
but it seems the loop is not waiting for the async call. When I am calling self.callTestOutputData(test_output_url) here, the loop is not waiting fpor the response but continuing for the next iteration and I am losing out the value to push into obj.requistion_number = testOutputResponse.value;
Please note : var results = response.results Here results is an array of Json objects.
Edit 1 : I tried forEach but that didn't work .
results.forEach(res => {
var obj = {}
obj.ferp = res.name;
// your code...
})
Original Code:
self.downloadDailyExcelProcurement = function (filters, excelTmpArr) {
self.disableExcelDownloadProcurement(true);
$('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label')[0].style.backgroundColor = "gray";
$('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label .demo-download-icon-24')[0].style.color = "#D8D8D8";
var payload = {};
if (typeof filters === "string") {
var fill = filters;
} else {
var fill = self.sendFilters();
if(self.app() === "fusion"){
fill += '&module=Procurement';
}else if (self.app() === "o2r"){
fill += '&module=O2r';
}
}
if(fill.includes("%3A")){
fill = fill.replace(/%3A/g, ':');
}
payload.Endpoint = 'executions/testcollection/' + fill;
//console.log(payload.Endpoint)
payload.BeforeSend = function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('guest:oracle123'));
$(".custom-loader-circle").show();
};
payload.OnSuccess = function (response) {
var results = response.results;
for (var res in results) {
var obj = {}
obj.ferp = results[res].name;
obj.po = "NA"
obj.receipt_no = "NA"
var test_output_url = results[res].reference_test_cases[0].automation_tests[0].test_outputs[0]
$.when(self.callTestOutputData(test_output_url)).done(function (testOutputResponse) {
if(testOutputResponse)
obj.requistion_number = testOutputResponse.value;
else {
obj.requistion_number = "NA";
}
self.excelTmpArr().push(obj);
});
}
else {
self.excelTmpArr().push(obj);
}
}
if (response.next) {
filters = ((response.next).split('testcollection'))[1];
if (filters[0] === "/") {
var test = filters.slice(1, filters.length);
}
self.downloadDailyExcelProcurement(test, self.excelTmpArr());
} else {
if (results.length === 0) {
$(".custom-loader-circle").hide();
self.disableExcelDownloadProcurement(false);
$('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label')[0].style.backgroundColor = "#4d0000";
$('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label .demo-download-icon-24')[0].style.color = "white";
showMessage(self.messages, "No Data to Download", '', 'info');
} else {
self.formatForExcel(self.excelTmpArr(), fill, "Procurement");
}
}
};
payload.OnError = function (data) {
showMessage(self.messages, data.status, data.statusText, 'error');
$(".custom-loader-circle").hide();
};
getData(payload);
}
Try using async and await :
async function asyncCall () {
// call here
}
for (var res in results) {
const response = await asyncCall();
}
var results = response.results;
if(result.length > 0){
results.map((data,index)=>{
//write your code here
})
}
This will help you ..
Use forEach() to iterate since it creates its own function closure:
results.forEach(res => {
var obj = {}
obj.ferp = res.name;
// your code...
})

Google function returning undefined

I have an issue with a custom google script I'm making to generate a bunch of sheets with info based on other sheets. I can't figure out why this is happening..
I've tried including logs and the values before the return is correct.. however when its returned, I get the value undefined.
it's regarding the function: getTournamentInfo(), called from tournamentInfo = getTournamentInfo(matchInfo[0]);
function getTournamentInfo(abbreviation) {
var sheet = ss.getSheetByName("Tournaments");
var tournaments = sheet.getRange("B2:B").getValues().filter(String);
console.log("Fetching Abbreviation: " + abbreviation);
var r = 2;
tournaments.forEach(function (tournament) {
if (tournament != "")
{
var tInfo = sheet.getRange("B"+r+":K"+r).getValues().toString().split(",");
if (tInfo[0] == abbreviation) {
console.log("Returning Info for: " + tInfo[0]);
return tInfo;
}
}
});
}
function generateSheets() {
var sheet = ss.getSheetByName("Match Schedule");
var matches = sheet.getRange("B5:B").getValues().filter(String);
var r = 5;
matches.forEach(function (match) {
if (match != "")
{
var matchInfo = sheet.getRange("B"+r+":L"+r).getValues().toString().split(",");
if (matchInfo[10] == "true") // Checks wether or not to generate the sheet
{
console.log("Generate = " + matchInfo[10]);
console.log("Fetching Tournament Info: " + matchInfo);
var tournamentInfo = "";
try {
tournamentInfo = getTournamentInfo(matchInfo[0]);
} catch (e) {
console.log(e);
}
console.log(tournamentInfo);
var template = "1v1PlayerTemplate"; // Default Template
if (tournamentInfo[3] == 2) {
template = "1v1TeamTemplate";
} else if (tournamentInfo[3] == 3) {
template = "XvXTeamTaplte";
}
var sheetName = matchInfo[0] + " | " + matchInfo[1];
var matchSheet = ss.getSheetByName(template).copyTo(ss.getSheetByName(template).getParent()).setName(sheetName);
}
}
r++;
});
}```
Your getTournamentInfo function is not returning your result. Your return statement only short-circuits the function supplied in forEach. This is one of the possible solutions (untested):
function getTournamentInfo(abbreviation) {
var sheet = ss.getSheetByName("Tournaments");
var tournaments = sheet.getRange("B2:B").getValues().filter(String);
console.log("Fetching Abbreviation: " + abbreviation);
var r = 2;
let result; // <----
tournaments.forEach(function (tournament) {
if (tournament != "" && result == undefined) { // <-----
var tInfo = sheet.getRange("B" + r + ":K" + r).getValues().toString().split(",");
if (tInfo[0] == abbreviation) {
console.log("Returning Info for: " + tInfo[0]);
result = tInfo; // <----
}
}
});
return result; // <----
}
You can do it more simply with a for loop, instead of forEach:
function getTournamentInfo(abbreviation) {
var sheet = ss.getSheetByName("Tournaments");
var tournaments = sheet.getRange("B2:B").getValues().filter(String);
console.log("Fetching Abbreviation: " + abbreviation);
var r = 2;
for (const tournament of tournaments) { // <==============
if (tournament != "") {
var tInfo = sheet.getRange("B" + r + ":K" + r).getValues().toString().split(",");
if (tInfo[0] == abbreviation) {
console.log("Returning Info for: " + tInfo[0]);
return tInfo;
}
}
}
}

protractor - how to return the boolean value using element.all

Iam a new bee to javascript , i tried to get the boolean value in side the if condition.Below are the code snippet ,
view.prototype.searchCustomer = function (customername) {
var customerFound = false;
var res = element.all(by.repeater('page in pages')).count().then(function (count) {
console.log("count of page count is " + count);
for (var i = 1; i <= count - 4; i++) {
element(by.xpath('//a[#class="ng-binding" and contains(text(),"rs")]'.replace("rs", i))).click();
element(By.xpath("//div[#class='cardHeader']/a[contains(text(),'" + customername + "')]")).isPresent().then(function (result) {
if (result) {
customerFound = true;
return result;
}
});
}
});
console.log("result is - "+customerFound);
};
Here console.log always returns false when the customerFound flag is true.Can any one help me on this please.I do have better knowledge on Java and learning javascript.
Thanks
Try to change var to let in a loop:
view.prototype.searchCustomer = function (customername) {
let customerFound = false;
const res = element.all(by.repeater('page in pages')).count().then(function (count) {
console.log("count of page count is " + count);
for (let i = 1; i <= count - 4; i++) {
element(by.xpath('//a[#class="ng-binding" and contains(text(),"rs")]'.replace("rs", i))).click();
element(By.xpath("//div[#class='cardHeader']/a[contains(text(),'" + customername + "')]")).isPresent().then(function (result) {
if (result) {
customerFound = true;
return result;
}
});
}
});
console.log("result is - "+customerFound);
};

toString method on Linked List implementation not working in js

I'm working through Cracking the Coding Interview and I thought I'd implement all the data structures in JS 5. Can anyone explain to me why my toString method isn't working?
Thanks!
function Node(data) {
this.next = null;
this.data = data;
}
Node.prototype.appendToTail = function(data) {
var end = new Node(data);
var n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
}
Node.prototype.toString = function(head) {
console.log(head)
if (head == null) {
return ""
} else {
return head.data.toString() + "-> " + head.next.toString();
}
}
var ll = new Node(1);
ll.appendToTail(3);
ll.appendToTail(4);
console.log(ll.toString())
function Node(data) {
this.next = null;
this.data = data;
}
Node.prototype.appendToTail = function(data) {
var end = new Node(data);
var n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
};
Node.prototype.toString = function() {
var returnValue = String(this.data);
if (this.next) {
returnValue = returnValue + "-> " + String(this.next);
}
return returnValue;
};
var ll = new Node(1);
ll.appendToTail(3);
ll.appendToTail(4);
console.log(String(ll))
or avoid this kind of problems completly and do not use prototype, class, this, call, etc
Your toString function takes an argument, but you're not passing it when you call toString.
If you want to access the node, you should use this, instead of passing in a value
Node.prototype.toString = function() {
var result = this.data.toString();
if (this.next) {
result += "-> " + this.next.toString();
}
return result;
}

Trouble with Node JS promises

I cannot find a solution to why this function returns before my message array is updated with the necessary values.
var calculateDistance = function (message, cLongitude, cLatitude, cSessionID) {
return new Promise(function (resolve, reject) {
distance.key = options.apiKey;
distance.units('metric');
var origins = [];
origins.push(cLatitude + ',' + cLongitude);
message.forEach(function (obj) {
obj.sessionId = cSessionID;
var destinations = [];
destinations.push(obj.geoLocation.latitude + ',' + obj.geoLocation.longitude);
distance.matrix(origins, destinations, function (err, distances) {
if (err) {
return console.log(err);
}
if (!distances) {
return console.log('no distances');
}
if (distances.status == 'OK') {
for (var i = 0; i < origins.length; i++) {
for (var j = 0; j < destinations.length; j++) {
var origin = distances.origin_addresses[i];
var destination = distances.destination_addresses[j];
if (distances.rows[0].elements[j].status == 'OK') {
var distance = distances.rows[i].elements[j].distance.text;
console.log('Distance from ' + origin + ' to ' + destination + ' is ' + distance);
obj.distance = distance;
} else {
console.log(destination + ' is not reachable by land from ' + origin);
obj.distance = 'N/A';
}
}
}
}
});
});
return resolve(message);
});
}
Could someone point out to me what i am doing wrong here.
Regards
Jimmy
var async = require('async');
var calculateDistance = function (message, cLongitude, cLatitude, cSessionID) {
return new Promise(function (resolve, reject) {
distance.key = options.apiKey;
distance.units('metric');
var origins = [];
origins.push(cLatitude + ',' + cLongitude);
async.each(message, function(obj, callback) {
obj.sessionId = cSessionID;
var destinations = [];
destinations.push(obj.geoLocation.latitude + ',' + obj.geoLocation.longitude);
distance.matrix(origins, destinations, function (err, distances) {
if (err) {
callback(err);
}
if (!distances) {
callback('no distances');
}
if (distances.status == 'OK') {
for (var i = 0; i < origins.length; i++) {
for (var j = 0; j < destinations.length; j++) {
var origin = distances.origin_addresses[i];
var destination = distances.destination_addresses[j];
if (distances.rows[0].elements[j].status == 'OK') {
var distance = distances.rows[i].elements[j].distance.text;
console.log('Distance from ' + origin + ' to ' + destination + ' is ' + distance);
obj.distance = distance;
} else {
console.log(destination + ' is not reachable by land from ' + origin);
obj.distance = 'N/A';
}
}
}
callback(null);
}
});
},function(err){
if(err){
return reject(err);
}else{
return resolve(message);
}
});
});
};
This is happening because your distance.matrix(origins, destinations, callback )is asynchronous . In above code distance.matrix method is getting pushed to event loop and continues it's execution and before that method callbacks gets executed resolve(message) is returned .
You need to read up on promises. It looks to me as if you are thinking of promises as magical way to set up a callback. "Magic" tends to mean "something I don't need to understand." In this case that's not true.
That executor function of yours (that is the function which begins with 'function(resolve,reject)') should set up one asynchronous request. If, as is normal, the request has a callback you put the 'resolve' and 'reject' in the callback. The result will be a promise object which has methods 'then' and 'catch' where your post-request processing goes.
Since you want to fill up a matrix with the results of a lot of async requests, you will need to read about 'Promise.all' so you can react when all of them have resolved.

Categories