node js async function won't stop - javascript

I am trying to break from the loop in an async function in node js, but it seems not to work. Once the script it's done, it starts executing again from the beginning. I can't use process.exit(), because then it won't save my logs.
Any idea how can I terminate this function?
Here is the code:
async function program() {
const xlsxFile = require('read-excel-file/node');
let rows = await xlsxFile('./file.xlsx', { sheet: 'sheet' });
const userRolesArray = [rows[0][1], rows[0][2]];
let roles = await getAllRoles();
let users = await getAllUsers();
let count = 0;
roleId = null;
for (userRole in userRolesArray) {
for (i in rows){
let someObject = {
test1: rows[i][0],
test1: rows[i][2],
};
if (someObject.test1 =='undefined') {
fs = require('fs')
await fs.appendFile('log_file.txt', i+")"+JSON.stringify(someObject)+"\n", function (err) {
if (err) throw err;
// console.log('Saved!');
});
} else {
//do something else;
}
}
}
count++;
if (userRole === '1') {
// loop has reached the end...Now exit
break;
}
};
program().then(() => {
return "Finished....!"
});

Related

Node.js How to retrieve data from http.get response

I am doing some practice in node.js. In this exercise I been asked to find a country name through a GET Http Request to an endpoint passing a page integer as a parameter.
Where the important response structs are these {page, total_pages, data}.
page is the current page,
total_pages is the last page,
data is an array of 10 country object.
In getCountryName func I am able to retrieve the right answer only if the answer is on the 1st page, the 1 iteration of the loop. So, why the loop only happens once?
Aditional, I wanted to retrieve the total_pages to replace the hardcode '25' value but I do not figure it out how to return it along with the search.
Any hint you wanna give me? The whole problem is in getCountryCode func.
'use strict';
const { Console } = require('console');
const https = require('https');
function makeRequest(page){
return new Promise(resolve => {
let obj='';
https.get('https://jsonmock.hackerrank.com/api/countries?page='+page, res => {
let data ='';
res.on('data',function(chunk){
data+=chunk;
});
res.on('end',function(){
obj=JSON.parse(data);
resolve(obj);
});
});
});
}
async function getCountryName(code) {
var res = '';
var pages = 25;
var i = 1;
while(i <= pages && res == ''){
console.log(i);
res = makeRequest(i)
.then(data => {
let f = ''
let p = data['total_pages'];
let search = data['data'].find(o => o.alpha3Code === code);
f = search != null ? search['name'] : f;
return f;
});
i++;
}
return res;
}
async function main() {
const name = await getCountryName('ARG');
console.log(`${name}\n`);
}
main();
Without modifying your code too much, this is how you do it:
'use strict';
const { Console } = require('console');
const https = require('https');
function makeRequest(page){
return new Promise(resolve => {
let obj='';
https.get('https://jsonmock.hackerrank.com/api/countries?page='+page, res => {
let data ='';
res.on('data',function(chunk){
data+=chunk;
});
res.on('end',function(){
obj=JSON.parse(data);
resolve(obj);
});
});
});
}
async function getCountryName(code) {
const pages = 25;
var i = 1;
let f = null
while(i <= pages && f === null){
console.log(i);
const data = await makeRequest(i) // put in try/catch
const p = data['total_pages'];
const search = data['data'].find(o => o.alpha3Code === code);
f = search !== null ? search['name'] : null;
i++;
}
return res;
}
async function main() {
const name = await getCountryName('ARG');
console.log(`${name}\n`);
}
main();

async error i want to download sequential with js [duplicate]

I have the following loop in node.js
for (var i in details) {
if (!details[i].AmntRcvd > 0) {
res.sendStatus(400);
return;
}
totalReceived += details[i].AmntRcvd;
UpdateDetail(details[i].PONbr, details[i].LineID).then((results) => {
console.log(results);
details[i].QtyOrd = results.QtyOrd;
details[i].QtyRcvd = results.QtyRcvd;
details[i].QtyPnding = results.QtyPnding;
details[i].UnitCost = results.UnitCost;
}).catch((error) => {
console.log(error);
});
}
The UpdateDetail function returns a promise. How do I wait for the promise to resolve/reject before moving on to the next iteration of the loop.
You can use the await keyword to solve this. more info here
async function main() {
for (var i in details) {
if (!details[i].AmntRcvd > 0) {
res.sendStatus(400);
return;
}
try {
totalReceived += details[i].AmntRcvd;
let results = await UpdateDetail(details[i].PONbr, details[i].LineID);
console.log(results);
details[i].QtyOrd = results.QtyOrd;
details[i].QtyRcvd = results.QtyRcvd;
details[i].QtyPnding = results.QtyPnding;
details[i].UnitCost = results.UnitCost;
}
catch(e) {
console.log(error);
}
}
}
You can use await:
for (var i in details) {
if (!details[i].AmntRcvd > 0) {
res.sendStatus(400);
return;
}
totalReceived += details[i].AmntRcvd;
await UpdateDetail(details[i].PONbr, details[i].LineID).then((results) => {
console.log(results);
details[i].QtyOrd = results.QtyOrd;
details[i].QtyRcvd = results.QtyRcvd;
details[i].QtyPnding = results.QtyPnding;
details[i].UnitCost = results.UnitCost;
}).catch((error) => {
console.log(error);
});
console.log('done with ' + i)
}
Here's the documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
You can use async library for this.then go for async.eachSeries.
You need to do npm install async first
Here is the example:
var async = require('async');
async.eachSeries(yourarray,function(eachitem,next){
// Do what you want to do with every for loop element
next();
},function (){
//Do anything after complete of for loop
})

Async function: for-Loop object empty after await statement

tldr at the bottom:
I don't really know how to explain my problem so I start with an example.
I have this async function (in reactJS but I think this is a JS related issue).
onUploadDrop = async (e, folderId) => {
e.preventDefault();
// check if the user uploaded files or folders
var uploadedItems = e.dataTransfer.items;
let files = [];
for (var i = 0; i < uploadedItems.length; i++) {
let item = uploadedItems[i].webkitGetAsEntry();
if (item.isDirectory) {
alert("is directory")
} else {
var file = await this.getFileByWebkitEntry(item);
files.push(file);
}
console.log(i);
}
// do something with files[]
}
This function is calling another async function:
getFileByWebkitEntry = async (item) => {
return new Promise(resolve => {
item.file(function (file) {
resolve(file);
}, function (err) {
console.log(err);
resolve("");
});
});
}
I'm looping through e.datatransfer.files which are basically some uploaded files or folders. Unfortunately this for-loop gets only executed once.
I did some debugging and found out that if I place a console.log before and after this line: var file = await ... This comes out:
tldr: After the await statement uploadedItems is empty thus ending the loop. Why is this happening?
I solved this by not using async - await but Promises instead.
It looks like this:
onUploadDrop = (e, folderId) => {
e.preventDefault();
// check if the user uploaded files or folders
var uploadedItems = e.dataTransfer.items;
let promises = [];
for (var i = 0; i < uploadedItems.length; i++) {
let item = uploadedItems[i].webkitGetAsEntry();
if (item.isDirectory) {
alert("is directory")
} else {
promises.push(this.getFileByWebkitEntry(item));
}
console.log(i);
}
Promise.all(promises).then(result => {
// do something with result (result = files)
});

How to prevent 'UnhandledPromiseRejection' on wrong user input?

I have 2 file, main.js and input.js.
In input.js I prompt the user to give me an interval in '1-10'(string) format. Later I cut this string and get the 2 numbers from it and check if the numbers are correct or not:
let getInput = () => {
return new Promise(function (resolve, reject) {
readline.question(`Give me interval (e.g.: 1-10).\n`, (input) => {
let fields = input.split('-');
let startNumber = Number(fields[0]);
let endNumber = Number(fields[1]);
if ((startNumber) && (endNumber) && (startNumber > 0) && (endNumber >= startNumber)) {
console.log(`ok`);
readline.close()
resolve([startNumber, endNumber]);
} else {
reject('not ok');
getInput();
}
});
});
}
In main.js I call this function asyncronously and I save it's result to a variable:
let intervalArray = await getInput();
.
.
.
someotherstuff
My problem is if I provide wrong input (for example '0-1' or '10-9' or '-10')
I got an UnhandledPromise error so the code won't execute more.
My goal would be when the user gives wrong input, 'not ok, give me another' should appear in console, and the program should wait for another input. If that input is correct, continue the execution. If not, the program should ask another input.
How could I achieve this?
EDIT:
Here are the full codes.
Input.js:
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
let getInput = () => {
return new Promise(function (resolve, reject) {
readline.question(`tol-ig formatumban (pl.: 1-10).\n`, (data) => {
let fields = data.split('-');
let startNumber = Number(fields[0]);
let endNumber = Number(fields[1]);
if ((startNumber) && (endNumber) && (startNumber > 0) && (endNumber >= startNumber)) {
console.log(`Kereses inditasa ${startNumber}-${endNumber} oldalakon.`);
readline.close()
resolve([startNumber, endNumber]);
} else {
readline.close();
reject(new Error('not ok'));
}
});
});
}
module.exports.getInput = getInput;
And main.js:
const puppeteer = require('puppeteer');
const { getInput } = require('./input');
const { mouseMovements } = require('./mouse');
const { tuneUserAgent } = require('./userAgent');
async function listItems() {
let intervalArray = null;
while (intervalArray === null) {
try {
let intervalArray = await getInput();
} catch (err) {
// write to user not ok
}
}
const browser = await puppeteer.launch({ headless: false });
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
const extractPartners = async url => {
const page = await context.newPage();
await tuneUserAgent(page);
await page.goto(url, { waitUntil: 'load' });
await page.waitFor(Math.round(Math.random() * 500) + 500);
await mouseMovements(page);
const partnersOnPage = await page.evaluate(() =>
Array.from(document.querySelectorAll("div.compact"))
.map(compact => (compact.querySelector(".logo a").href.slice(-16))));
await page.close();
const nextPageNumber = parseInt(url.match(/page=(\d+)$/)[1], 10) + 1;
if (nextPageNumber > endPage) {
console.log(`Terminate recursion on: ${url}`);
return partnersOnPage;
} else {
console.log(`Scraped: ${url}`);
const nextUrl = `https://marketingplatform.google.com/about/partners/find-a-partner?page=${nextPageNumber}`;
let randomWait = (Math.round(Math.random() * 2000) + 1000);
await page.waitFor(randomWait);
return partnersOnPage.concat(await extractPartners(nextUrl));
}
};
let startPage = intervalArray[0];
let endPage = intervalArray[1];
const firstUrl =
`https://marketingplatform.google.com/about/partners/find-a-partner?page=${startPage}`;
const partners = await extractPartners(firstUrl);
await browser.close();
return Promise.resolve(partners);
};
module.exports.listItems = listItems;
You have to handle the error. As you are using await, the easiest is to use try catch.
try {
let intervalArray = await getInput();
} catch (err) {
// write to user not ok
}
You can wrap it in cycle to keep requesting user for new inputs.
let intervalArray = null;
while (intervalArray === null) {
try {
let intervalArray = await getInput();
} catch (err) {
// write to user not ok
}
}
^^ Remember to remove getInput(); from the else part of your new Promise. Maybe you will also need to close the readline as you will open it again. Also rejection is similar to throwing an error and you should always send Error-based object there.
} else {
readline.close();
reject(new Error('not ok'));
}
EDIT: after your update I created POC. The readline.close() should not be there actually (looks its one time use only), but this POC looks fine:
toBeRequired.js
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
let getInput = () => {
return new Promise(function (resolve, reject) {
readline.question(`tol-ig formatumban (pl.: 1-10).\n`, (data) => {
let fields = data.split('-');
let startNumber = Number(fields[0]);
let endNumber = Number(fields[1]);
if ((startNumber) && (endNumber) && (startNumber > 0) && (endNumber >= startNumber)) {
console.log(`Kereses inditasa ${startNumber}-${endNumber} oldalakon.`);
readline.close()
resolve([startNumber, endNumber]);
} else {
reject(new Error('not ok'));
}
});
});
}
module.exports.getInput = getInput;
server.js
const a = require('./toBeRequired');
async function x() {
let input = null;
while (input === null) {
try {
input = await a.getInput();
} catch (err) {
console.log('nooo');
}
}
}
x();
Create these two files in same folder and run node server.js

Foreach with Promise not waiting on method results

I am trying to iterate through the JSON files generated by the protractor tests. I pull all the file names into an array and call a method that opens and parses through the each file, post the results to the database and pass back a passed/failed flag.
I have tried all the examples here
Make angular.forEach wait for promise after going to next object and still get the same results.
The method is actually called, but the results are not posted to the db. I have tested the parser.parseResults on an individual file and it successfully posted to the db, so it has to have something to do with the promise not resolving correctly.
Is it not possible to do something like this in the jasmine/protractor framework? Or do I have something wrong in the code?
I have included the code for my latest attempt.
Thank You
Christine
matches.reduce(function (p, val) {
console.log('val', val);
return p.then(function () {
return parser.parseResults(val);
});
}, Promise.resolve()).then(function (finalResult) {
console.log('finalResult = ', finalResult);
}, function (err) {
console.log('error in reduce',err);
});
parser.parseResults code
protractorParser.prototype.parseResults = function (fileName) {
return new Promise((resolve, reject) => {
console.log('In parseresults', fileName);
json.readFile(fileName, function (err, obj) {
try {
if (err != null) {
console.log('error reading file',err);
reject(err);
}
console.log('obj - ',obj);
var results = [];
var Passed = 0;
var Message = '';
var Stack = '';
for (var suite in obj) {
var specs = obj[suite].specs;
console.log('spec - ', specs);
if (specs.length > 0) {
for (var i = 0; i < specs.length; i++) {
var assert = specs[i];
var tcR = new RegExp(/TC[\d]+/);
var tc = assert.description.match(tcR);
if (!assert.failedExpectations.length) {
Passed = 1;
}
else {
assert.failedExpectations.forEach((expectation) => {
Message = expectation.message;
Stack = expectation.stack.split('\n')[1].trim();
})
Passed = 0;
}
if (tc != null) {
utility.TestDataManager.insertAutomationResults(tc[0], assert.description, Passed, process.env.testBuild,
'P', Message, Stack, 0, moment().utcOffset(config.get('settings.timeOffset')).format('YYYY-MM-DDTHH:mm:ss'), '')
.then(function (resp) {
resolve(Passed);
}, (err) => {
console.log('Posting to Database failed ', err);
reject(err);
});
} else {
console.log('no test case found for test: ' + assert.description + ' -- skipping');
reject(err);
}
}
}
}
}
catch (err) {
console.log('rejecting opening file');
reject(err);
}
});
})
}
If there is not exactly one suite in the obj, with exactly one spec, then your promise is either resolved not at all or multiple times.
Avoid wrapping too many things in the new Promise constructor - always promisify on the smallest possible level, and use promise chaining afterwards.
protractorParser.prototype.parseResults = function (fileName) {
return new Promise((resolve, reject) => {
console.log('In parseresults', fileName);
json.readFile(fileName, function (err, obj) {
if (err != null) {
console.log('error reading file', err);
reject(err);
} else {
resolve(obj);
}
});
}).then(function(obj) {
console.log('obj - ',obj);
var results = [];
for (var suite in obj) {
var specs = obj[suite].specs;
console.log('spec - ', specs);
for (let i = 0; i < specs.length; i++) {
const assert = specs[i];
const tcR = /TC[\d]+/;
const tc = assert.description.match(tcR);
let Passed = 1;
let Message = '';
let Stack = '';
if (assert.failedExpectations.length) {
const expectation = assert.failedExpectations[assert.failedExpectations.length-1];
Passed = 0;
Message = expectation.message;
Stack = expectation.stack.split('\n')[1].trim();
}
if (tc != null) {
const time = moment().utcOffset(config.get('settings.timeOffset')).format('YYYY-MM-DDTHH:mm:ss');
const promise = utility.TestDataManager.insertAutomationResults(tc[0], assert.description, Passed, process.env.testBuild, 'P', Message, Stack, 0, time, '');
results.push(promise.catch(err => {
console.log('Posting to Database failed ', err);
throw err;
}));
} else {
console.log('no test case found for test: ' + assert.description + ' -- skipping');
// I don't think you want to `throw err` here, right?
}
}
}
return Promise.all(results);
});
};

Categories