Yandex Cloud
Search
Discuss with expertTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
  • Marketplace
    • Featured
    • Infrastructure & Network
    • Data Platform
    • AI for business
    • Security
    • DevOps tools
    • Serverless
    • Monitoring & Resources
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
    • Price calculator
    • Pricing plans
  • Customer Stories
  • Documentation
  • Blog
© 2026 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparing with other Yandex Cloud services
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Error handling
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes
  • FAQ

In this article:

  • Handler types
  • Asynchronous handler
  • Synchronous handler
  • Examples
  • HTTP request structure output
  • Asynchronous handler
  • Synchronous handler
  1. Developing in Node.js
  2. Request handler

Request handler for a function in Node.js

Written by
Yandex Cloud
Improved by
Anton P.
Updated at July 2, 2026
View in Markdown
  • Handler types
  • Asynchronous handler
  • Synchronous handler
  • Examples
    • HTTP request structure output
    • Asynchronous handler
    • Synchronous handler

A request handler is a method used to process each Node.js function call. When creating a function version, you should must the entry point that consists of the file name and the request handler name, e.g., index.myFunction. To enable external access to the handler (file), export it by adding a key to the module.exports object.

Note

At any given time, one function instance cannot handle more calls than set in the concurrency parameter. This allows you to use global variables without the need to ensure data integrity control.

When calling the handler, the runtime provides the following arguments:

  1. Request body (the body parameter).

    If the request body is a JSON document, it will be converted into Object using the JSON.parse method.

  2. Invocation context (the context parameter).

    The context provides all required information about the function version. The structure of this object is described in Node.js function invocation context.

Handler typesHandler types

A function can handle one or multiple requests at the same time, with synchronous and asynchronous handlers used respectively.

Asynchronous handlerAsynchronous handler

A handler can be an async function() asynchronous function. In this case, you can use the following statements:

  • return: Returns the function response.
  • throw: Reports an error to the runtime.
  • await: Tracks the execution of asynchronous function invocations.

Note

An asynchronous function must return a direct result. You do not need to additionally use the Promise object to track the call results because the function is already asynchronous.

For more information about the development process using async/await, see this JavaScript tutorial.

Synchronous handlerSynchronous handler

If you do not need to invoke asynchronous functions or you need to use a traditional callback model, use a synchronous handler.

To return the execution result, use the return statement or throw an exception using the throw statement. A synchronous function must return a result different from undefined; otherwise, the code will fail with this error: Non-async entry point should provide a result. Return a value or use async function instead.

However, if the handler returns a Promise, the runtime will automatically wait for it to resolve. When using a Promise object, correct error and exception handling depends on your function code. Make sure that:

  • One of the function callbacks (resolve or reject) within the Promise object will always be executed.
  • All exceptions in the function body are handled correctly.
    Otherwise, code execution will be disrupted: the function will stop responding to invocations or the entire process will fail.

For more information about the development process using Promise, see this JavaScript tutorial.

ExamplesExamples

HTTP request structure outputHTTP request structure output

The following function outputs the request structure and invocation context to both the execution log and function response:

module.exports.handler = function (event, context) {
    console.log("event", event);
    console.log("context", context);
    
    return {
        statusCode: 200,
        body: JSON.stringify({
            event: event,
            context: context
        })
    };
};

Asynchronous handlerAsynchronous handler

The function sends a request to https://example.com/ and returns the HTTP status code and the contents of the JSON document:

module.exports.handler = async function (event) {
    
    const response = await fetch('https://example.com/');
    const body     = await response.text();
    
    return {
        code: response.status,
        body: body
    };
    
};

Synchronous handlerSynchronous handler

The function returns either a prepared response or an error:

module.exports.handler = function () {
    if (Math.random() >= 0.5) {
        throw new Error("not so lucky");
    }

    return {
        "message": "Lucky one!"
    };
};

Was the article helpful?

Previous
Managing dependencies
Next
Invocation context
© 2026 Direct Cursus Technology L.L.C.