Using async/await and try/catch to make tiered api calls - javascript

SO friends!
I need to make a call to an api; if it fails, I need to make a call to same api with different param; if it fails AGAIN, I need to make a call to same api with a third, different param; if it fails finally after that, is an actual error and can bugger out.
Only way I can figure to do this is with nested try/catch statements, ala:
const identityCheck = async (slug) => {
let res;
try {
res = await Bundle.sdk.find(slug);
} catch (err) {
console.log('Fragment didn\'t work ========', slug, err);
try {
res = await Bundle.sdk.find(`package/${slug}`);
} catch (e) {
console.log('Fragment didn\'t work package ========', e);
try {
res = await Bundle.sdk.find(`${slug}-list`);
} catch (error) {
console.log('None of the fragments worked================.', error);
}
}
}
return logResponse(res);
};
identityCheck('fashion');
But seems like there MUST be another simpler way to do this. I tried boiling down into a retry function, but that just ends up being even more code and way less clear:
const identityCheck = (slug) => {
const toTry = [
slug,
`package/${slug}`,
`${slug}-list`
];
return new Promise((resolve, reject) => {
let res;
let tryValIndex = 0;
const attempt = async () => {
try {
res = await Bundle.sdk.find(toTry[tryValIndex]);
return resolve(logResponse(res));
} catch (err) {
console.log(`toTry ${toTry[tryValIndex]} did not work ========`, slug, err);
if (tryValIndex >= toTry.length) {
return reject(new Error('Everything is broken forever.'));
}
tryValIndex++;
attempt();
}
};
attempt();
});
};
Guidance and opinions appreciated!

Avoid the Promise constructor antipattern, and use a parameter instead of the outer-scope variable for the recursion count:
function identityCheck(slug) {
const toTry = [
slug,
`package/${slug}`,
`${slug}-list`
];
async function attempt(tryIndex) {
try {
return await Bundle.sdk.find(toTry[tryIndex]);
} catch (err) {
console.log(`toTry ${toTry[tryIndex]} did not work ========`, slug, err);
if (tryIndex >= toTry.length) {
throw new Error('Everything is broken forever.'));
} else {
return attempt(tryIndex+1);
}
}
}
return attempt(0);
}

Following on Bergi's answer, but trying to preserve the original structure to avoid "even more code":
const idCheck = async (slug, alsoTry = [`package/${slug}`, `${slug}-list`]) => {
let res;
try {
res = await Bundle.sdk.find(slug);
} catch (err) {
if (!alsoTry.length) throw err;
return idCheck(alsoTry.shift(), alsoTry);
}
return logResponse(res);
};
idCheck('fashion');
This takes advantage of default arguments being quite powerful.
Same complexity, but aesthetically closer to the nested try-blocks, a simpler pattern perhaps.

Related

Why can't I return data after await a promise object in async function

I'm a newbie studying coding.
I tried to mock mysql database query, so I followed below code(https://techhelpnotes.com/node-js-mock-database-jest-using-ts-jest-utils/)
//database.js
const getDbConnection = () => {
const pool = mysql.createPool(DB_CONFIG);
return {
query : function (sql) {
return util.promisify(pool.query).call(pool, sql);
};
//controller.js
try {
let result = await db.query(sql);
res.status(200).json(result);
} catch (error) {
console.log(DB error, error);
}
It worked for me, but I thought I could return the query with await by using it in databse.js like the one below
query : async function(sql) {
try {
return await util.promisify(pool.query).call(pool, sql);
} catch (error) {
console.log(DB error, error);
}
}
so I thought I can use query function without error handling
let result = await db.query(sql);
But it doesn't work. What difference is there between the two codes that makes the above code work and the below now??
Many thanks!

Typescript: Variable is used before being assigned in strict mode

I'm trying to use database transaction to create a Page record however I'm getting Variable 'createdPage' is used before being assigned even though this.pagesService.create() only returns Page and it will throw error if something goes wrong so program can be sure that createdPage is set if no exception is thrown. So why I'm getting this error?
#Post('')
async create(
#Body() body: PageCreateDto,
): Promise<Page> {
let createdPage: Page;
try {
await this.database.transaction(async trx => {
createdPage = await this.pagesService.create(body, trx);
});
} catch (error) {
throw new InternalServerErrorException('unable to create page');
}
return createdPage;
}
The problem is that the function you pass into the transaction call doesn't get run synchronously and so you can't be sure that createdPage is actually assigned when you return it. You could solve this by creating a promise.
#Post('')
async create(#Body() body: PageCreateDto): Promise<Page> {
return new Promise<Page>((resolve, reject) => {
try {
await this.database.transaction(trx => this.pagesService
.create(body, trx)
.then(resolve));
} catch (error) {
reject(new InternalServerErrorException('unable to create page'));
}
});
}
Returning it inside arrow function solved the issue:
#Post('')
async create(
#Body() body: PageCreateDto,
): Promise<Page> {
let createdPage: Page;
try {
createdPage = await this.database.transaction(async trx => {
return this.pagesService.create(body, trx);
});
} catch (error) {
throw new InternalServerErrorException('unable to create page');
}
return createdPage;
}

How can I return an error from a function?

Let's say I have a function like this:
const getPlayer = (id) => {
return players[id;]
}
//--------------------------
const client = getPlayer(9);
How can I return the err parameter to the client variable if no player is found? For example:
if (client.err) {
//do something
}
I tried passing the error via throw new Error('my error') , but the function still doesn't get it, what am I doing wrong?:(
So your first instinct was correct, you should use the 'throw' keyword to raise an error. To act on the error you need to use try/catch like I've done below.
const getPlayer = (id) => {
if(id in players) {
return players[id];
}
throw new Error("Oh noes...!");
}
try {
const client = getPlayer(9);
} catch(error) {
console.log(error.message);
}
When an error is thrown inside a function being executed in a try block, execution immediately jumps to the catch block, allowing you to respond to the error appropriately.
Checkout try/catch syntax for that.
For example:
const getPlayer = (id) => {
if (!id) {
throw new Error('no id provided');
}
return players[id]
}
To get this "error" state, when it triggers you can do following:
try {
const client = getPlayer(null);
} catch(error) {
console.log(error.message);
}
I have tried something but not sure if this is what you are after:
let a = (x) => {
if (x == 0) {
throw new Error("Votes are zero");
} else {
return x;
}
};
Run it in the console with the values as a(0) --> will throw you a new error and a(5)

Class/ function result use in following code

I am a beginner in javascript and am now trying to understand the subject of classes. I've defined the following class. Now I would like to use the result outside of this in a variable.
The class looks like this:
class Maad{
constructor(name, NumWeek, NumMonth){
this.name =name;
this.NumWeek = NumWeek;
this.NumMonth = NumMonth;
}
queryMaad(){
const mongodb =require('mongodb');
const client = require('mongodb').MongoClient;
const url= 'mongodb://localhost:27017/vertrieb';
client.connect(url,(error, db) =>{
if(!error){
console.log("month_log steht")
};
let col = db.collection("umsatz5");
col.aggregate([{'$match': {'AD': this.name}}, {'$match': {'Kalenderwoche':this.NumWeek}}, {'$count': 'procjetnumber'}],function(err, result){
if (err) {
console.error("Error calling", err);
}
console.log(result[0].projectnumber);
result[0].projectnumber;
})
db.close();
});
}
}
My request is:
let ma_1 = new Maad("Hans Wurst", NumWeek);
ma_1.queryMaad();
How can I save the result (the number of projects) in a variable to use it outside of the class? Thanks for your help.
In general, you would assign it basically the way that you assign anything:
const ma_1 = new Maad("Hans Wurst", NumWeek);
const myVar = ma_1.queryMaad();
However your method is a void method which doesn't return anything, so you need to edit your class if you want to get the number of projects.
Returning something from the function is harder than it sounds because MongoClient.connect is a void method which uses callbacks rather than returning a Promise of a response. Honestly I would recommend using a library like mongoose. But it is possible to make the method asynchronous ourselves by returning a new Promise which we resolve or reject based on the callbacks.
class Maad {
constructor(name, NumWeek, NumMonth) {
this.name = name;
this.NumWeek = NumWeek;
this.NumMonth = NumMonth;
}
async queryMaad() {
const url = "mongodb://localhost:27017/vertrieb";
return new Promise((resolve, reject) => {
MongoClient.connect(url, (error, db) => {
if (error) {
reject(error);
}
console.log("month_log steht");
let col = db.collection("umsatz5");
col.aggregate(
[
{ $match: { AD: this.name } },
{ $match: { Kalenderwoche: this.NumWeek } },
{ $count: "procjetnumber" }
],
function (err, result) {
if (err) {
reject(err);
}
resolve(result);
}
);
db.close();
});
});
}
}
Now queryMaad is an async method, one which returns a Promise. That Promise will resolve to the result of col.aggregate on success (you could also resolve to result[0].projectnumber). The promise will reject if there is an error in the connect or col.aggregate methods.
You now get your value like so (probably inside of a function which is itself async):
const result = await ma_1.queryMaad();
You can catch rejection errors here, or allow them to be thrown and catch them higher up.
try {
const result = await ma_1.queryMaad();
} catch (error) {
// console.error or whatever
}

Convert Ember function to use ES2017 async/await

I'd like to convert this Ember route action to use ES2017 async / await. Can someone please explain what this would look like?
Per spec, I already added: babel: { includePolyfill: true } to my ember-cli-build.js file:
save() {
let tenant = this.modelFor(this.routeName).tenant;
let app = this.modelFor(this.routeName).app;
return tenant.save().then(()=> {
return tenant.get('app').save({ adapterOptions: { tenantId: tenant.id }}).then(() => {
this.transitionTo('tenants.tenant.info', tenant.id);
}).catch((error) => {
tenant.get('app').rollback();
throw error;
});
}).catch((error) => {
tenant.rollback();
throw error;
});
}
Your code, converted to async/await:
async save() {
let tenant = this.modelFor(this.routeName).tenant;
let app = this.modelFor(this.routeName).app;
try {
await tenant.save();
try {
await tenant.get('app').save({ adapterOptions: { tenantId: tenant.id }});
this.transitionTo('tenants.tenant.info', tenant.id);
} catch (error) {
tenant.get('app').rollback();
throw error;
}
} catch (error) {
tenant.rollback();
throw error;
}
}
To convert from promises, you add the await keyword to method calls that return promises. Everything that you place in the then method of the promise you can simply put after the await statement.
The promises' catch methods convert to regular try/catch blocks.
Very similar to the answer Patrick Hund wrote, but attaching the catch statements to the promises being awaited, rather than wrapping in try/catch blocks, and extracting the error logic into a single function.
async save() {
let tenant = this.modelFor(this.routeName).tenant;
let app = this.modelFor(this.routeName).app;
await tenant.save().catch(handleError.bind(this, false));
await tenant.get('app').save({ adapterOptions: { tenantId: tenant.id }})
.catch(handleError.bind(this, true));
this.transitionTo('tenants.tenant.info', tenant.id);
// isApp is bound via bind; error will be provided when called via catch
function handleError(isApp, error) {
if (isApp) {
tenant.get('app').rollback();
} else {
tenant.rollback();
}
throw error;
}
}

Categories

Resources