Skip to content

A complete example for this lib please #218

Answered by Papooch
kasir-barati asked this question in Q&A
Discussion options

You must be logged in to vote

I'll take a closer look later, but @UseCls is essentially just a syntactic sugar over ClsService#run.

Whatever gets returned from the function passed to run gets also returned into the call site of run.

async getUser() {
  const user = await this.cls.run(
    async ()=> {
      return await this.db.getUser()
    }
  )
  return user
}

is equivalent to

async getUser() {
  return this.getUserFromDb()
}

@UseCls()
async getUserFromDb() {
  return this.db.getUser()
}

However, you should keep in mind tha the @UseCls decorator is meant to be used as the entry point to the context, and the context should be set up as early as possible. You can't really pass variables to @UseCls, it can only read …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kasir-barati
Comment options

@Papooch
Comment options

Answer selected by kasir-barati
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants