Timeout
Setting request timeout with minimum effort

Overview

SugoiJS provides an easy way to set the timeout for a request based on the endpoint using the @Timeout decorator.

@Timeout decorator

Using the @Timeout decorator we can set a timeout on a method and define a callback in case of a timeout.
Timeout(ms: number, onTimeout?: (req, res) => void)

Example

1
export class TimeoutMethod{
2
@HttpGet('/timeout')
3
@Timeout(1000, (request, response) => {
4
console.error("timeout called")
5
response.json({timeout: true})
6
})
7
public async timeout() {
8
return await new Promise(resolve => {
9
setTimeout(() => {
10
resolve({data: new Date()})
11
}, 2000)
12
}
13
}
Copied!
Last modified 2yr ago