英文:
typescript function is not returning the value but it is getting consoled
问题
以下是您要翻译的内容:
我对TypeScript相当新,我已经创建了一个函数,该函数在我尝试在控制台输出时运行正常,但在从其他函数调用它时不返回任何值
返回值的函数
static generateToken = async (user: any, expires: any): Promise<string> => {
try {
const config: any = await configHelper.getConfigData();
const conData: string = JSON.parse(config);
const configdata = JSON.parse(conData);
const payload = {
sub: user.contactId,
iat: moment().unix(),
exp: expires.unix(),
user: user,
};
console.log('token one', Jwt.sign(payload, configdata.jwt.secret))
return Jwt.sign(payload, configdata.jwt.secret);
} catch (err) {
console.log('Error', err)
return "Error"
}
};
调用该函数的函数,我尝试在所有可能的位置放置await,但仍然处于挂起状态,但它已经被记录在控制台中
请注意,我已根据您的要求仅返回翻译后的部分。如果您需要更多帮助,请随时提问。
英文:
I am pretty new to typescript i have create the function that return the access token when i try consoling it was working fine but it not returing any value when i call from other function
Function that return's value
static generateToken = async (user: any, expires: any):Promise<string>
=> {
try{
const config: any = await configHelper.getConfigData();
const conData: string = JSON.parse(config);
const configdata = JSON.parse(conData);
const payload = {
sub: user.contactId,
iat: moment().unix(),
exp: expires.unix(),
user: user,
};
console.log('token one',Jwt.sign(payload, configdata.jwt.secret))
return Jwt.sign(payload, configdata.jwt.secret);
}catch(err){
console.log('Error',err)
return "Error"
}
};
Function where we call i have tried putting await in all the possible places but still i gets pending but it is getting consoled
static createToken = async (userDetails: any) => {
const config: any = await configHelper.getConfigData();
const conData: string = JSON.parse(config);
const configdata = JSON.parse(conData);
const accessTokenExpires = moment().add(
configdata.jwt.accessExpirationMinutes,
"minutes"
);
const accessToken = {
token: this.generateToken(userDetails, accessTokenExpires),
expires: accessTokenExpires.toDate(),
};
console.log('$$$$$$$$$$$$$$',accessToken)
const refreshTokenExpires = moment().add(
configdata.jwt.refreshExpirationMinutes,
"minutes"
);
const refreshToken = {
token: this.refreshTokenGenerate(userDetails, refreshTokenExpires),
expires: refreshTokenExpires.toDate(),
};
return {
accessToken,
refreshToken,
};
};
答案1
得分: 1
generateToken
声明为 async
。这意味着在调用它时,您必须使用 await
。
const accessToken = {
token: await this.generateToken(userDetails, accessTokenExpires),
// ^ 在这里添加了 await
expires: accessTokenExpires.toDate(),
};
英文:
generateToken
is declared as async
. Which means you must await
when you call it.
const accessToken = {
token: await this.generateToken(userDetails, accessTokenExpires),
// ^ added await here
expires: accessTokenExpires.toDate(),
};
答案2
得分: 1
我看不到实际的generateToken
调用处有任何await
。你需要在那里等待,以便代码等待承诺解决。
const accessToken = {
token: await this.generateToken(userDetails, accessTokenExpires),
expires: accessTokenExpires.toDate(),
};
我建议你理解async和await的工作原理,而不是试图随机添加“在所有可能的地方添加await”这样的方式,因为这样做只会减慢你的速度并引起挫折感。
简而言之,
“async和await使承诺更容易编写”
async使函数返回一个Promise
await使函数等待一个Promise
英文:
I don't see any await
at the actual generateToken
call. You would need to await there for the code to wait for the promise to resolve.
const accessToken = {
token: await this.generateToken(userDetails, accessTokenExpires),
expires: accessTokenExpires.toDate(),
};
I would suggest you understand how async and await work instead of trying to randomly add "await in all possible places" as brute forcing your way like this will only slow you down and cause frustration.
In short,
> "async and await make promises easier to write"
> async makes a function return a Promise
> await makes a function wait for a Promise
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论