*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to make callbacks run in the background

On this page

How to make callbacks run in the background
===========================================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [Callbacks](/v0.2/docs/concepts/#callbacks)

By default, LangChain.js callbacks are blocking. This means that execution will wait for the callback to either return or timeout before continuing. This is to help ensure that if you are running code in [serverless environments](https://en.wikipedia.org/wiki/Serverless_computing) such as [AWS Lambda](https://aws.amazon.com/pm/lambda/) or [Cloudflare Workers](https://workers.cloudflare.com/), these callbacks always finish before the execution context ends.

However, this can add unnecessary latency if you are running in traditional stateful environments. If desired, you can set your callbacks to run in the background to avoid this additional latency by setting the `LANGCHAIN_CALLBACKS_BACKGROUND` environment variable to `"true"`. You can then import the global [`awaitAllCallbacks`](https://api.js.langchain.com/functions/langchain_core_callbacks_promises.awaitAllCallbacks.html) method to ensure all callbacks finish if necessary.

To illustrate this, we’ll create a [custom callback handler](/v0.2/docs/how_to/custom_callbacks) that takes some time to resolve, and show the timing with and without `LANGCHAIN_CALLBACKS_BACKGROUND` set. Here it is without the variable set:

    import { RunnableLambda } from "@langchain/core/runnables";Deno.env.set("LANGCHAIN_CALLBACKS_BACKGROUND", "false");const runnable = RunnableLambda.from(() => "hello!");const customHandler = {  handleChainEnd: async () => {    await new Promise((resolve) => setTimeout(resolve, 2000));    console.log("Call finished");  },};const startTime = new Date().getTime();await runnable.invoke({ number: "2" }, { callbacks: [customHandler] });console.log(`Elapsed time: ${new Date().getTime() - startTime}ms`);

    Call finishedElapsed time: 2005ms

And here it is with backgrounding on:

    import { awaitAllCallbacks } from "@langchain/core/callbacks/promises";Deno.env.set("LANGCHAIN_CALLBACKS_BACKGROUND", "true");const startTime = new Date().getTime();await runnable.invoke({ number: "2" }, { callbacks: [customHandler] });console.log(`Initial elapsed time: ${new Date().getTime() - startTime}ms`);await awaitAllCallbacks();console.log(`Final elapsed time: ${new Date().getTime() - startTime}ms`);

    Initial elapsed time: 0msCall finishedFinal elapsed time: 2098ms

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You’ve now learned how to run callbacks in the background to reduce latency.

Next, check out the other how-to guides in this section, such as [how to create custom callback handlers](/v0.2/docs/how_to/custom_callbacks).

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to attach callbacks to a module

](/v0.2/docs/how_to/callbacks_attach)[

Next

How to pass callbacks into a module constructor

](/v0.2/docs/how_to/callbacks_constructor)

*   [Next steps](#next-steps)