What is the use of resolve in promise in nodejs - node.js

function getAuthSecrets() {
return new Promise((resolve, reject) => {
let authSecrets = NamespaceManager.getNamespace('******).get('authSecrets');
if (!authSecrets) {
sm.getSecret(authConfig.secretName).then((secret) => {
NamespaceManager.getNamespace('cls2_context').set('authSecrets', secret);
resolve(secret);
}).catch((error) => {
reject(error);
});
} else {
resolve(authSecrets);
}
});
}
Here it gets called :
if (username && password) {
getAuthSecrets().then(secrets => {
What will get returned by calling getAuthSecret?

authSecrets or secret, depending on which call to resolve gets executed.
You want to return resolve(...) to avoid writing code that tries to resolve twice.
That you are catching then immediately rejecting is a sign that your machinery is more complex than it needs to be. You can lift a value to the Promise context with Promise.resolve:
function getAuthSecrets() {
const authSecrets = NamespaceManager.getNamespace('******).get('authSecrets');
return authSecrets ? Promise.resolve(authSecrets) :
sm.getSecret(authConfig.secretName).then((secret) => {
NamespaceManager.getNamespace('cls2_context').set('authSecrets', secret);
return Promise.resolve(secret);
})
});
}

Related

Node.js asynchronous and synchronous integration

In short, I am making a Discord bot with discord.js and I am having trouble with asynchronous and synchronous functions.
In order to assign variables, the last part of the function loops through each variable and then converts it to its desired type, which can be seen here:
argsList.forEach((argument, index) => {
let finalArgument = argument
const type = args[index].type
if (type === UserArgument) {
new UserArgument(argument).result
.then(userObject => {
finalArgument = userObject
})
.catch(error => {
throw error
})
} else if (type === MemberArgument) {
new MemberArgument(argument, guild).result
.then(memberObject => {
finalArgument = memberObject
})
.catch(error => {
throw error
})
} else if (type === ChannelArgument) {
new ChannelArgument(argument, guild).result
.then(channelObject => {
finalArgument = channelObject
})
.catch(error => {
throw error
})
} else if (type === RoleArgument) {
new RoleArgument(argument, guild).result
.then(roleObject => {
finalArgument = roleObject
})
.catch(error => {
throw error
})
}
finalArgList.push(finalArgument)
})
return finalArgList
}
And here is an example of how the UserArgument class looks like (all other argument bases basically look the same)
class UserArgument {
constructor(user) {
this.result = new Promise((resolve, reject) => {
if (typeof(user) === "string") {
if (user.match(/^<#!([0-9]+)>$/)) {
user = user.match(/[0-9]+/)
}
if (isNumeric(user)) {
ArgumentBase.client.users.fetch(user)
.then(userObject => {
resolve(userObject)
return userObject
})
}
this.#getUserFromName(user)
.then(userObject => {
resolve(userObject)
return userObject
})
} else if (user instanceof DiscordJS.User) {
resolve(user)
return user
} else if (user instanceof DiscordJS.GuildMember || user instanceof DiscordJS.ThreadMember) {
resolve(user.user)
return user.user
}
let userObject = ArgumentBase.client.users.resolve(user)
if (userObject) {
resolve(userObject)
return userObject
}
reject(new UserNotFound(toString(user)))
})
}
async #getUserFromName(username) {
return new Promise((resolve, reject) => {
for (const user in ArgumentBase.client.users.cache) {
if (user.username === username) {
resolve(user)
return user
}
}
throw new UserNotFound(username)
})
}
}
The issue that I am coming across is that the code that handles each argument does not wait for the function to be finished and instead skips over it. This of course causes the command to be executed before the arguments are even processed. For my tests, I was testing throwing errors from the UserArgument class, and the error did get thrown, but only after the command had already executed because that is when it decided to finish.
My assumption is that since Promise is an asynchronous function, the code keeps running and does not wait for it. I tried turning the argument function to an async function and use await, but I kept getting the SyntaxError: await is only valid in async functions and the top level bodies of modules, even when the function is an async function (function declaration is static async getCommandArgs(invokedString, args, guild = undefined)). If someone could help me, that would be amazing. Thank you for your time and help.

SyntaxError: Unexpected reserved word "await", node.js is correct version

I am trying to run a function asynchronously 20 times.
I have the function definition:
import axios from 'axios';
async function updateUser (url, user) {
return new Promise((resolve, reject) => {
axios.put(url, JSON.stringify(user))
.then(response => {
//Folio returned code 204
console.log('The user has been updated');
resolve(response.data);
}).catch((err) => {
//Folio returned error
console.error(`Error Text: ${err.response.data}`);
console.error(`Error Code: ${err}`);
});
});
};
export {updateUser};
Then I am trying to loop through users in an external JSON, and update them to my system 20 at a time:
import {updateUser} from './updateUser.js';
import usersjson from './jsons/users.json';
let promises=[];
if (usersjson.users.length) {
try {
for (const user of usersjson.users) {
update = new promise ((resolve,reject) => {
updateUser(url, user)
.then((list) => {resolve(list)})
.catch((error)=>{reject(error)})
});
promises.push(update);
if (promises.length = 20) {
await Promise.all(promises)
.then((responses)=> {
console.log(responses);
promises=[];
}).catch((err)=> {
console.log(err);
});
}
}
} catch(err)=> {
console.log(err);
}
}
However, I am getting an error message in the console:
await Promise.all(promises)
^^^^^
SyntaxError: Unexpected reserved word
My node version is 12.19.0 (based on previous questions, it seems it has to be at least 10, so that shouldn't be the issue).
When I run the function without await, they work correctly (i.e. the users are updated on my system).
It also seems that the for loop is asynchronous by itself, but I'm afraid that if there will be too many calls simultaneously my system will block the API, so I want to make 20-50 calls at a time, tops.
In order to use await you need to declare the function in which you are putting the await as async
For example:
const functionReturningPromise = (input) => {
return new Promise((resolve, reject) => {
if (!input) {
return reject();
}
return resolve();
});
}
const functionAwaitingPromise = async () => {
for (let i = 0; i < 20; i +=1) {
try {
await functionReturningPromise(i);
console.log(i);
} catch (error) {
console.log(error);
}
}
}

Handling nested promises

I am trying to reject a value inside a nested promise but it doesn't seem to actually reject it correctly. In the code below, when I get an error from promiseVariable, it doesn't reject with the error. In the promiseVariable.catch statement, I have reject(err). Shouldn't that reject with that error for the whole promise?
return new Promise((resolve, reject) => {
const user = anotherFunction();
if (!user) {
promiseVariable.then((data) => {
user = data;
}).catch((err) => {
reject(err)
})
}
resolve(user);
});
Because it will start the promiseVariable chain and jump over to resolve. In the .then, you should resolve there, or put an else.
return new Promise((resolve, reject) => {
const user = anotherFunction();
if (!user) {
promiseVariable
.then(resolve)
.catch(reject);
} else {
resolve(user);
}
});
It seems you're over-promising, if that's a word. You can just "await" for your other promise to complete.
Here, anotherFunction is a simulated Promise, where after 3 seconds it returns a valid object for your conditional if(!user).
You can try changing resolve({user: 1234}) with a false (or falsy) value to see its rejection.
function anotherFunction() {
return new Promise((resolve,reject) => {
setTimeout(() => resolve({user: 1234}), 3000)
});
}
async function mainFunction() {
const user = await anotherFunction();
return user || {error: 404};
}
mainFunction().then(result => console.log(result));

use answer .then () outside and use in another part of the code in node js

How can I use the answer of a promise outside of. Then what should I do?
arreglo.forEach((item) => {
if (item.tipoCampo == 3) {
self.campoSelects(item.tablaCampo)
.then(resp => {
console.log(resp)
})
.catch(e => console.log(e))
}
});
console.log (resp) inside the .then () knows it and prints correctly, but when I want to know resp out of the forEach to use below, it says undefined
Thanks.
arreglo.forEach((item) => {
if (item.tipoCampo == 3) {
self.campoSelects(item.tablaCampo)
.then(resp => {
logMyData(resp);
})
.catch(e => console.log(e))
}
});
logMyData=(x)=>{
console.log(x);
}
This is just as simple as adding a helper function which executes inside your .then
Guessing that you want to be able to access the value within the forloop. Since self.campoSelects is a promise we can use async await.
// Call campo selects
function getCampoSelects(_self, tablaCampo) {
return new Promise(async (resolve, reject) => {
let campoData;
try {
campoData = await _self.campoSelects(tablaCampo);
} catch (err) {
reject(err);
}
resolve(campoData);
});
}
function happyLittleFunc() {
const arreglo = [];
arreglo.forEach(async (item) => {
if (item.tipoCampo === 3) {
let campoSelect;
// Unsure if you are setting self somewhere but it can be passed in here.
try {
campoSelect = await getCampoSelects(self, item.tipoCampo);
} catch (err) {
console.log(err);
return;
}
console.log(campoSelect);
}
});
}
happyLittleFunc();

Using async await properly in node js

To overcome callback hell in javascript, I'm trying to use async await from legacy code written in SQLServer procedure.
But I'm not sure my code might be write properly.
My first confusing point is when async function returns, should it return resolve() as boolean, or just return reject and handle with try-catch?
Here is my code snippets.
Please correct me to right direction.
apiRoutes.js
app.route('/api/dansok/cancelDansok')
.post(dansokCancelHandler.cancelDansok);
dansokCancelController.js
const sequelize = models.Sequelize;
const jwt = require('jsonwebtoken');
async function jwtAccessAuthCheck(accessToken) {
if (!accessToken) {
return Promise.reject('Empty access token');
}
jwt.verify(accessToken,"dipa",function(err){
if(err) {
return Promise.reject('TokenExpiredError.');
} else {
return Promise.resolve();
}
});
}
async function checkFeeHist(dansokSeqNo) {
let feeHist = await models.FeeHist.findOne({
where: { DansokSeqNo: dansokSeqNo}
});
return !!feeHist;
}
async function getNextDansokHistSerialNo(dansokSeqNo) {
....
}
async function getDansokFee(dansokSeqNo) {
....
}
async function doCancel(dansokSeqNo) {
try {
if (await !checkFeeHist(dansokSeqNo)) {
log.error("doCancel() invalid dansokSeqNo for cancel, ", dansokSeqNo);
return;
}
let nextDansokSerialNo = await getNextDansokHistSerialNo(dansokSeqNo);
await insertNewDansokHist(dansokSeqNo, nextDansokSerialNo);
await updateDansokHist(dansokSeqNo);
await updateVBankList(dansokSeqNo, danokFee.VBankSeqNo);
await getVBankList(dansokSeqNo);
} catch (e) {
log.error("doCancel() exception:", e);
}
}
exports.cancelDansok = function (req, res) {
res.setHeader("Content-Type", "application/json; charset=utf-8");
const dansokSeqNo = req.body.DANSOKSEQNO;
const discKindCode = req.body.HISTKIND;
const worker = req.body.PROCWORKER;
const workerIp = req.body.CREATEIP;
const accessToken = req.headers.accesstoken;
//check input parameter
if (!dansokSeqNo || !discKindCode || !worker || !workerIp) {
let e = {status:400, message:'params are empty.'};
return res.status(e.status).json(e);
}
try {
jwtAccessAuthCheck(accessToken)
.then(() => {
log.info("jwt success");
doCancel(dansokSeqNo).then(() => {
log.info("cancelDansok() finish");
res.status(200).json({ message: 'cancelDansok success.' });
});
});
} catch(e) {
return res.status(e.status).json(e);
}
};
You'll need to rewrite jwtAccessAuthCheck(accessToken) so that it keeps track of the outcome of its nested tasks. In the code you've written:
// Code that needs fixes!
async function jwtAccessAuthCheck(accessToken) {
// This part is fine. We are in the main async flow.
if (!accessToken) {
return Promise.reject('Empty access token');
}
// This needs to be rewritten, as the async function itself doesn't know anything about
// the outcome of `jwt.verify`...
jwt.verify(accessToken,"dipa",function(err){
if(err) {
// This is wrapped in a `function(err)` callback, so the return value is irrelevant
// to the async function itself
return Promise.reject('TokenExpiredError.');
} else {
// Same problem here.
return Promise.resolve();
}
});
// Since the main async scope didn't handle anything related to `jwt.verify`, the content
// below will print even before `jwt.verify()` completes! And the async call will be
// considered complete right away.
console.log('Completed before jwt.verify() outcome');
}
A better rewrite would be:
// Fixed code. The outcome of `jwt.verify` is explicitly delegated back to a new Promise's
// `resolve` and `reject` handlers, Promise which we await for.
async function jwtAccessAuthCheck(accessToken) {
await new Promise((resolve, reject) => {
if (!accessToken) {
reject('Empty access token');
return;
}
jwt.verify(accessToken,"dipa",function(err){
if(err) {
reject('TokenExpiredError.');
} else {
resolve();
}
});
});
// We won't consider this async call done until the Promise above completes.
console.log('Completed');
}
An alternate signature that would also work in this specific use case:
// Also works this way without the `async` type:
function jwtAccessAuthCheck(accessToken) {
return new Promise((resolve, reject) => {
...
});
}
Regarding your cancelDansok(req, res) middleware, since jwtAccessAuthCheck is guaranteed to return a Promise (you made it an async function), you'll also need to handle its returned Promise directly. No try / catch can handle the outcome of this asynchronous task.
exports.cancelDansok = function (req, res) {
...
jwtAccessAuthCheck(accessToken)
.then(() => {
log.info("jwt success");
return doCancel(dansokSeqNo);
})
.then(() => {
log.info("cancelDansok() finish");
res.status(200).json({ message: 'cancelDansok success.' });
})
.catch(e => {
res.status(e.status).json(e);
});
};
I strongly suggest reading a few Promise-related articles to get the hang of it. They're very handy and powerful, but also bring a little pain when mixed with other JS patterns (async callbacks, try / catch...).
https://www.promisejs.org/
Node.js util.promisify

Resources