...

/

Solution: Chain Multiple Decorators on a Service Method

Solution: Chain Multiple Decorators on a Service Method

Compose multiple decorators to enhance a function with both logging and timing, without touching its core logic.

We'll cover the following...

Solution explanation

  • Lines 2–8: We define fetchUserData, an async function that simulates a delayed API response. It serves as the unmodified “core logic.”

  • Lines 11–21: We build the first decorator, withLogging, which wraps the original function and returns a new async function that logs input arguments before the call and the returned value afterward.

    • We define a wrapper function wrappedFn, which preserves the behavior of the original.

    • To avoid losing the function name during composition, we ...