In this article we will learn about some of the frequently asked TypeScript programming questions in technical like “limit number of open sockets axios” Code Answer. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on typescript was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in Typescript. Below are some solution about “limit number of open sockets axios” Code Answer.
limit number of open sockets axios
xxxxxxxxxx
1
import axios from 'axios'
2
3
const MAX_REQUESTS_COUNT = 5
4
const INTERVAL_MS = 10
5
let PENDING_REQUESTS = 0
6
7
// create new axios instance
8
const api = axios.create({})
9
10
/**
11
* Axios Request Interceptor
12
*/
13
api.interceptors.request.use(function (config) {
14
return new Promise((resolve, reject) => {
15
let interval = setInterval(() => {
16
if (PENDING_REQUESTS < MAX_REQUESTS_COUNT) {
17
PENDING_REQUESTS++
18
clearInterval(interval)
19
resolve(config)
20
}
21
}, INTERVAL_MS)
22
})
23
})
24
25
/**
26
* Axios Response Interceptor
27
*/
28
api.interceptors.response.use(
29
function (response) {
30
PENDING_REQUESTS = Math.max(0, PENDING_REQUESTS - 1)
31
return Promise.resolve(response)
32
},
33
function (error) {
34
PENDING_REQUESTS = Math.max(0, PENDING_REQUESTS - 1)
35
return Promise.reject(error)
36
})
37
38
export default api