1[← Concurrent](8-Concurrent-EN.md) | Asynchronous Call[(中文)](9-Asynchronous-CN.md) | [Package Management →](10-Package-Management-EN.md)
2***
3## Asynchronous Call
4
5### Open asynchronous call
6Alibaba Cloud Go SDK supports opening asynchronous calls in two ways:
7Note: After opening the asynchronous call, you need to call `Shutdown()` before you can start the asynchronous call again
8
91. Enable asynchronous call when initializing `client`
10   ```go
11   import (
12       "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
13       "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
14       "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
15   )
16
17   c := sdk.NewConfig()
18   c.EnableAsync = true // Asynchronous task switch
19   c.GoRoutinePoolSize = 10 // Number of goroutines
20   c.MaxTaskQueueSize = 20 // Maximum number of tasks for a single goroutine
21   c.Timeout = 10 * time.Second
22   credential := credentials.NewAccessKeyCredential("acesskeyid", "accesskeysecret")
23   client, err := ecs.NewClientWithOptions("regionid", c, credential)
24   ```
25
262. Enable asynchronous call when calling `EnableAsync`
27   ```go
28   // the first parameter is the maximum number of goroutines
29   // the second parameter is the maximum number of tasks for a single goroutine
30   // EnableAsync is only allowed to be called once, unless the Shutdown() method is used to first close the asynchronous call, and then call EnableAsync
31   client.EnableAsync(10, 20)
32   ```
33
34### Make an asynchronous call
35Alibaba Cloud Go SDK supports asynchronous calls in two ways:
36
371. Using channel as return values
38    ```go
39    responseChannel, errChannel := client.FooWithChan(request)
40
41    // this will block
42    response := <-responseChannel
43    err = <-errChannel
44    ```
45
462. Use callback to control the callback
47
48    ```go
49    blocker := client.FooWithCallback(request, func(response *FooResponse, err error) {
50        // handle the response and err
51    })
52
53    // blocker which is type of (chan int),is used to control synchronization,when returning 1 means success,and returning 0 means failure.
54    // When <-blocker returns failure,err also will be handled by afferent callback.
55    result := <-blocker
56    ```
57
58### Close asynchronous call
59Use the function of `client` to close asynchronous call and the goroutines
60   ```go
61   client.Shutdown()
62   ```
63
64***
65[← Concurrent](8-Concurrent-EN.md) | Asynchronous Call[(中文)](9-Asynchronous-CN.md) | [Package Management →](10-Package-Management-EN.md)