TypeScript函数没有返回值,但它已经被记录在控制台中。

huangapple go评论64阅读模式
英文:

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&lt;string&gt;
=&gt; {
    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(&#39;token one&#39;,Jwt.sign(payload, configdata.jwt.secret))
      return Jwt.sign(payload, configdata.jwt.secret);
    }catch(err){
      console.log(&#39;Error&#39;,err)
      return &quot;Error&quot;
    }
      };

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) =&gt; {
    const config: any = await configHelper.getConfigData();
    const conData: string = JSON.parse(config);
    const configdata = JSON.parse(conData);
    const accessTokenExpires = moment().add(
      configdata.jwt.accessExpirationMinutes,
      &quot;minutes&quot;
    );
    const accessToken = {
      token: this.generateToken(userDetails, accessTokenExpires),
      expires: accessTokenExpires.toDate(),
    };
    console.log(&#39;$$$$$$$$$$$$$$&#39;,accessToken)
    const refreshTokenExpires = moment().add(
      configdata.jwt.refreshExpirationMinutes,
      &quot;minutes&quot;
    );
    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

huangapple
  • 本文由 发表于 2023年1月10日 02:03:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75061272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定