Skip to content

Documentation / @andrew_l/context / bindContext

Function: bindContext() ​

bindContext<T>(fn): () => T

Binds the current context to the provided function. Useful for creating callbacks with the current context, such as setTimeout or EventEmitter handlers.

Type Parameters ​

T ​

T

Parameters ​

fn ​

() => T

Returns ​

(): T

Returns ​

T

Example ​

ts
const main = withContext(() => {
  provide("user", { id: 1, name: "Andrew" });

  setInterval(bindContext(() => {
    const user = inject("user");
    console.log(user); // { id: 1, name: 'Andrew' }
  }));
});

main();