amaster
amaster

Reputation: 2163

Declaring an async function in an existing object

Here is what I have already:

myFunct({ myObj: { db } })

I need to add another function in such as:

myFunct({ myObj: async ({ req }) => {
  //more scripts
} })

What I tried and failed:

myFunct({ myObj: {
  db,
  async (req) => {
    //more scripts
  }
} })

At the => I get the syntax error:

Unexpected token, expected {

Upvotes: 2

Views: 117

Answers (2)

JeromeBu
JeromeBu

Reputation: 1159

You did not give a key to your function :

Try :

myFunct({
  myObj: {
    db,
    yourKey: async (req) => {
      //more scripts
    }
  }
})

Upvotes: 3

Quentin
Quentin

Reputation: 944005

You have to supply a property name.

If you have a variable, it can act as both the property name and value.

const myFunction = async (req) => {
    //more scripts
};

myFunct({ myObj: {
  db,
  myFunction
} })

If you only have a value, then you need to state the property name explicitly.

myFunct({ myObj: {
  db,
  myFunction: async (req) => {
    //more scripts
  }
} })

Upvotes: 3

Related Questions