All Products
Search
Document Center

Alibaba Cloud SDK:Configure a timeout period

Last Updated:Jul 09, 2024

This topic describes how to configure timeout periods in Alibaba Cloud SDK V1.0 for Node.js.

Configuration method

Note

The timeout periods configured in Alibaba Cloud SDK V1.0 for Node.js take effect in the following descending order: the timeout periods that you configure by invoking the request function, the timeout periods that you configure by initiating the SDK client, and the default timeout periods.

  • The default timeout period is 3,000 milliseconds.

  • The following code provides an example on how to configure a timeout period when you invoke the request function.

    const RPCClient = require('@alicloud/pop-core').RPCClient;
    
    const client = new RPCClient({
        // Obtain the AccessKey ID of the Resource Access Management (RAM) user from an environment variable.
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        // Obtain the AccessKey secret of the RAM user from an environment variable.
        accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
        endpoint: 'https://ecs.cn-beijing.aliyuncs.com',
        apiVersion: '2014-05-26',
    });
    
    const params = {};
    const action = 'DescribeRegions';
    const opts = {
        // Configure a timeout period for connection requests.
        connectTimeout: 3000,
        // Configure a timeout period for read requests.
        readTimeout: 3000,
        // Configure a timeout period for connection requests and read requests.
        timeout: 3000
    };
    client.request(action, params, opts).then((result) => {
        console.log(JSON.stringify(result));
    });
    
  • The following code provides an example on how to configure a timeout period when you initialize the SDK client.

    const RPCClient = require('@alicloud/pop-core').RPCClient;
    
    const opts = {
        // Configure a timeout period for connection requests.
        connectTimeout: 3000,
        // Configure a timeout period for read requests.
        readTimeout: 3000,
        // Configure a timeout period for connection requests and read requests.
        timeout: 3000
    };
    const client = new RPCClient({
        // Obtain the AccessKey ID of the RAM user from an environment variable.
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        // Obtain the AccessKey secret of the RAM user from an environment variable.
        accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
        endpoint: 'https://ecs.cn-beijing.aliyuncs.com',
        apiVersion: '2014-05-26',
        opts: opts,
    });
    
    const params = {};
    const action = 'DescribeRegions';
    
    client.request(action, params).then((result) => {
        console.log(JSON.stringify(result));
    });