Problems with promises in JavaScript - javascript

The last line of code doesn't work and I don't know how to fix it. I understand that second "then" has to return resolve() but how can I realize it?
let getNumber = new Promise((resolve) => {
//API
EthereumNote.getAmountOfMyNotes(function(error, result) {
if (!error) {
let AmountOfMyNotes = Number(result)
resolve(AmountOfMyNotes)
console.log(result)
} else
console.error(error)
})
}).then(result => {
return new Promise((resolve) => {
for (let i = 0, p = Promise.resolve(); i < result; i++) {
p = p.then(_ => new Promise(resolve => {
//API
EthereumNote.getMyNote(i, function(error, result) {
if (!error) {
let text = String(result[0])
let noteID = Number(result[1])
console.log(text)
console.log(noteID)
resolve()
} else
console.error(error)
})
}));
}
})
}).then(() => console.log('Hi!')) // this one doesn't work

Avoid the Promise constructor antipattern! It's ok to make a new Promise to wrap EthereumNote.getMyNote, it's not ok to wrap the loop. You never resolve() that outer promise.
Instead, just return the promise chain you created in p:
let getNumber = new Promise((resolve) => {
//API
EthereumNote.getAmountOfMyNotes(function(error, result) {
if (error) reject(error);
else resolve(Number(result));
})
}).then(amountOfMyNotes => {
console.log(amountOfMyNotes);
var p = Promise.resolve();
for (let i = 0; i < amountOfMyNotes; i++) {
p = p.then(_ => new Promise((resolve, reject) => {
//API
EthereumNote.getMyNote(i, function(error, result) {
if (error) reject(error);
else resolve(result);
});
})).then(result => {
let text = String(result[0])
let noteID = Number(result[1])
console.log(text)
console.log(noteID))
});
}
return p;
}).then(() => {
console.log('Hi!'); // this one now works
}, err => {
console.error(err);
});

Related

check if user in group ActiveDirectory.js node.js

In c# I would write something like this:
bool HasRole(string userName, string[] groupNames)
{
var ad = new ActiveDirectory();
return groupsNames.Any(groupName => ad.IsUserInGroup(userName, groupName);
}
and then just
if (HasRole("UserName", new[] {"group1", "group2"}))
//do something
in javascript it looks like all things are doing asynchronously, I've read about promises and so on, and tried this:
const ActiveDirectory = require('activedirectory2');
const Promise = require('promise');
const globals = require('./globals');
const activeDirectory = new ActiveDirectory(globals.AdConfig);
hasRole(msg, ...groupNames) {
if (groupNames == null || groupNames == undefined || groupNames.length == 0)
return false;
let promises = [];
groupNames.forEach(groupName => {
let promise = new Promise((resolve, reject) => {
activeDirectory.isUserMemberOf(msg.envelope.user.name, groupName, function (err, isMember) {
if (err) {
reject(err)
}
resolve(isMember);
});
});
promises.push(promise);
});
//I don't clearly understand how I can return promise result only or promise with the right result
let hasRole = false;
promises.forEach(promise => promise.done(result => {
if (result)
hasRole = result;
}));
return hasRole;
So how can write something like:
if (hasRole(msg, 'group1', 'group2'))
//do something...
I suppose I should return promise but how I can do it if i need to check multiple groups?
UPDATE
I wrapped the forEach loop in promise:
return new Promise((resolve, reject) => {
promises.forEach(promise => promise.done(result => {
if (result)
return resolve(result);
}));
and then:
hasRole(msg, 'group1', 'group2').done(result => {
if (result)
//do...
});
May there is another way?
Found a solution. Promises.all is what i needed.
Result code:
hasRole(msg, ...groupNames) {
let promises = [];
groupNames = groupNames.filter(x => x.trim().length > 0);
if (groupNames == undefined || groupNames.length == 0)
{
promises.push(new Promise.resolve(false));
return Promise.all(promises);
}
groupNames.forEach(groupName => {
let promise = new Promise((resolve, reject) => {
activeDirectory.isUserMemberOf(msg.envelope.user.name, groupName, function (err, isMember) {
if (err) {
reject(err)
}
resolve(isMember);
});
});
promises.push(promise);
});
return Promise.all(promises);
}

AWS Lambda function handler not inserting to Athena

I'm using a snippet example for Amazon Athena just to test inserting some data. I can't tell why it isn't working and CloudWatch logs does not show any output when the statement execution is completed. Even when I change it to a simple select statement I can't see any output. I know the query, database and table is fine, because when I test it using the Athena query editor it executes without a problem.
module.exports.dlr = async event => {
let awsFileCreds = {
accessKeyId: "XXX",
secretAccessKey: "XXX"
};
let creds = new AWS.Credentials(awsFileCreds);
AWS.config.credentials = creds;
let client = new AWS.Athena({ region: "eu-west-1" });
let q = Queue((id, cb) => {
startPolling(id)
.then(data => {
return cb(null, data);
})
.catch(err => {
console.log("Failed to poll query: ", err);
return cb(err);
});
}, 5);
const sql = "INSERT INTO delivery_receipts (status, eventid, mcc, mnc, msgcount, msisdn, received, userreference) VALUES ('TestDLR', 345345, 4353, '5345435', 234, '345754', 234, '8833')"
makeQuery(sql)
.then(data => {
console.log("Row Count: ", data.length);
console.log("DATA: ", data);
})
.catch(e => {
console.log("ERROR: ", e);
});
function makeQuery(sql) {
return new Promise((resolve, reject) => {
let params = {
QueryString: sql,
ResultConfiguration: { OutputLocation: ATHENA_OUTPUT_LOCATION },
QueryExecutionContext: { Database: ATHENA_DB }
};
client.startQueryExecution(params, (err, results) => {
if (err) return reject(err);
q.push(results.QueryExecutionId, (err, qid) => {
if (err) return reject(err);
return buildResults(qid)
.then(data => {
return resolve(data);
})
.catch(err => {
return reject(err);
});
});
});
});
}
function buildResults(query_id, max, page) {
let max_num_results = max ? max : RESULT_SIZE;
let page_token = page ? page : undefined;
return new Promise((resolve, reject) => {
let params = {
QueryExecutionId: query_id,
MaxResults: max_num_results,
NextToken: page_token
};
let dataBlob = [];
go(params);
function go(param) {
getResults(param)
.then(res => {
dataBlob = _.concat(dataBlob, res.list);
if (res.next) {
param.NextToken = res.next;
return go(param);
} else return resolve(dataBlob);
})
.catch(err => {
return reject(err);
});
}
function getResults() {
return new Promise((resolve, reject) => {
client.getQueryResults(params, (err, data) => {
if (err) return reject(err);
var list = [];
let header = buildHeader(
data.ResultSet.ResultSetMetadata.ColumnInfo
);
let top_row = _.map(_.head(data.ResultSet.Rows).Data, n => {
return n.VarCharValue;
});
let resultSet =
_.difference(header, top_row).length > 0
? data.ResultSet.Rows
: _.drop(data.ResultSet.Rows);
resultSet.forEach(item => {
list.push(
_.zipObject(
header,
_.map(item.Data, n => {
return n.VarCharValue;
})
)
);
});
return resolve({
next: "NextToken" in data ? data.NextToken : undefined,
list: list
});
});
});
}
});
}
function startPolling(id) {
return new Promise((resolve, reject) => {
function poll(id) {
client.getQueryExecution({ QueryExecutionId: id }, (err, data) => {
if (err) return reject(err);
if (data.QueryExecution.Status.State === "SUCCEEDED")
return resolve(id);
else if (
["FAILED", "CANCELLED"].includes(data.QueryExecution.Status.State)
)
return reject(
new Error(`Query ${data.QueryExecution.Status.State}`)
);
else {
setTimeout(poll, POLL_INTERVAL, id);
}
});
}
poll(id);
});
}
function buildHeader(columns) {
return _.map(columns, i => {
return i.Name;
});
}
return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
Figured it out. Using aws lambda events with athena is easy using the athena-express package. You can specify your configuration and query the athena database like you normally would with significantly less code than what's provided in the amazon athena nodejs example.
This is the code I used to achieve a result:
"use strict";
const AthenaExpress = require("athena-express"),
aws = require("aws-sdk");
const athenaExpressConfig = {
aws,
db: "messaging",
getStats: true
};
const athenaExpress = new AthenaExpress(athenaExpressConfig);
exports.handler = async event => {
const sqlQuery = "SELECT * FROM delivery_receipts LIMIT 3";
try {
let results = await athenaExpress.query(sqlQuery);
return results;
} catch (error) {
return error;
}
};

What is the proper way to use Promise.reject with javascript

I have this following piece of code
new Promise((resolve, reject) => {
resolve(apiRequest(data))
reject(console.log('Error'))
}).then(response)
Both methods (resolve and reject) are being fired but I want to call reject only when something goes wrong.
How can I throw an error if something goes wrong on that case?
I checked that but it seems like I can not use an If statement to do that check.
new Promise((resolve, reject) => {
const printResult = apiRequest(data)
console.log(printResult) //Outputs Promise {<pending>}
resolve(printResult) //Then it works
reject(console.log('Error'))
}).then(response)
What would be the correct approach to reject a promise?
The easiest way would be with an if condition. i.e
new Promise((resolve, reject) => {
// do something...
if(somethingGoodHappened) {
resolve(data)
} else {
reject(error)
}
})
But usually when dealing with async requests, the thing you are calling will often be returning a promise, so you can attach the then and catch callbacks there.
apiRequest(data)
.then((result) => {
// all good
})
.catch((err) => {
console.log(err)
})
const mock_api = () => new Promise((res, rej) => {
const number = Math.floor((Math.random() * 100) + 1);
setTimeout(() => {
if (number%2==0) return res('randomly RESOLVED')
return rej('randomly REJECTED')
}, 2000)
})
const async_promise = () => new Promise(async (resolve, reject) => {
try {
const resolvedPromise = await mock_api()
resolve(resolvedPromise)
} catch (e) {
reject(e)
}
})
const classicPromise = () => new Promise((resolve, reject) => {
mock_api()
.then(resolve)
.catch(reject)
})
const makeAsyncRequest = async () => {
try {
const data = await async_promise()
console.log('ASYNC AWAIT RESOLVE', data)
} catch (e) {
console.log('ASYNC AWAIT ERR', e)
}
}
makeAsyncRequest()
classicPromise()
.then(r => console.log('PROMISE CHAIN RESOLVE', r))
.catch(e => console.log('PROMISE CHAIN ERR', e))
Because of you resolve before reject so it cannot run into reject,
You can use:
if (printResult) {
resolve(printResult)
} else {
reject(console.log('Error'))
}
You can catch exceptions and return them as rejected Promises
function asyncFunc() {
try {
doSomethingSync();
return doSomethingAsync()
.then(result => {
ยทยทยท
});
} catch (err) {
return Promise.reject(err);
}
}
Always check for err if there is any err return a promise (example below)
// Return new promise
return new Promise(function(resolve, reject) {
// Do async job
request.get(options, function(err, resp, body) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(body));
}
})
})

nodejs how to use multiple await promises

how can i use multi promise await in my codes ? when i wanna use second await for second promise it throw an error
function ReadJSONFile() {
return new Promise((resolve, reject) => {
fs.readFile('import.json', 'utf-8', (err, data) => {
if (err) reject(err);
resolve(JSON.parse(data));
});
});
}
const Get_Image = async (Path) => {
Child_Process = exec('node get_image.js "'+Path+'",(err,stdout,stderr) =>
return new Promise((resolve,reject) => {
resolve(stdout);
});
}
const Catch = async () => {
let get_json_file = await ReadJSONFile(); // this works perefectly
for(var i=0;i< Object.keys(get_json_file);i++) {
console.log(await Get_Image(get_json_file[i].image_path); //but this throw error
}
}
you didn`t return a promise that is why you got an error
const Get_Image = async (Path) => {
return new Promise((resolve,reject) => {
Child_Process = exec('node get_image.js "'+Path+'",(err,stdout,stderr) =>
resolve(stdout);
});
});
}

Returning empty response using ES6 promise in node.js

how can I call a query using ES6 native promises in node.js. Below is the code.
let arr= [];
conn.query('select * from table1', (err, b) => {
for (let i = 0; i < b.length; i++) {
console.log(b[i]["id"])
let promiseGetData = new Promise((resolve, reject) => {
conn.query('select * from table2 where id = ?', [b[i]["id"]], (err, ce) => {
if (err) {
const response = {
statusCode: 500,
body: JSON.stringify({
message: err
}),
}
reject(response);
} else {
if (ce.length != 0) {
resolve(ce);
}
}
});
});
promiseGetData .then((data) => {
b[i]['data'] = data;
arr.push(b[i])
}).catch((err) => {
console.log(err);
});
}
console.log(b)
})
I see an empty response when i do console.log(b). I dont know if I have used a promise in correct way, I think for first query I should execute in the promise as well. Any help is very much appreciated
Wrapping an async callback-based function into a promise is called Promisifying.
You can of course use a library for that, but basically what it does is :
const queryAsPromise = function( ...args ) {
return new Promise( function( resolve, reject ) {
try {
conn.query(...args, function( error, result ) {
if ( error ) {
reject( error );
} else {
resolve( result );
}
} );
} catch( error ) {
reject( error );
}
} )
} );
By doing this one time you will keep your code DRY and you can always use that promise for making queries now :
queryAsPromise('select * from table1')
.then( result => {
return Promise.all(
result.map( b => {
return queryAsPromise('select * from table2 where id = ?', b["id"])
.then( data => b["data"] = data )
} )
)
)
.catch( err => res.send(500) )
.then( console.log )
You got empty response from console.log(b) because the promise from querying database aren't finished. You have to wait all of them to finish before you can get the full result.
Sample:
let arr = [];
conn.query('select * from table1', (err, b) => {
var promiseArr = [];
for (let i = 0; i < b.length; i++) {
let promiseGetData = new Promise((resolve, reject) => {
conn.query('select * from table2 where id = ?', [b[i]["id"]], (err, ce) => {
if (err) {
const response = {
statusCode: 500,
body: JSON.stringify({
message: err
}),
}
reject(response);
} else {
if (ce.length != 0) {
resolve(ce);
}
}
});
});
promiseArr.push(promiseGetData);
}
Promise.all(promiseArr).then((resultArr) => {
//resultArr is all the resolved value returned from the promise in promiseArr
for (let i = 0; i < resultArr.length; i++) {
b[i]['data'] = resultArr[i];
arr.push(b[i]);
}
}).then(() => {
console.log(arr);
}).catch((err) => {
//if any promise throw/reject with error, it will go here directly
console.log(err);
});
})
Edit:
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Categories

Resources