Home > OS >  How to correctly return from function
How to correctly return from function

Time:12-20

How to correctly return apiKey from User.ts createUser function to Test.ts in my case?

User.ts

interface User {
  url: string,
  name: string,

}
class User{
  async createUser(
   user: User
  ):Promise<void> { 
    let apiKey = await user.getUserApiKey(user.name);
    console.log(`${user.name} api key - ${apiKey} received`);
return apiKey;
    }
}

Test.ts

test("Smoke Test", async (t) => {
console.log(${apiKey} - User api key)
}

CodePudding user response:

You may have to specify Promise<void> to the correct type of apiKey. It may look like Promise<String>, or leave the return type specification empty (i.e. remove the : Promise<void> from the createUser method).

In the Test.ts you have to create a User object first and call the createUser Method on this object.

test("Smoke Test", async (t) => {
    let user = new User();
    user.name = "theName";
    user.url = "theURL";
    let apiKey = await user.createUser(user);
    console.log(${apiKey} - User api key)
}

It seems like, the createUser method is not well designed. Is it intentional to have a (possible different) user parameter, although it is called on a user object?

  • Related