Skip to main content

Concurrency Control

Control how multiple HTTP requests are handled when sent simultaneously. Keq provides two handling strategies: serial and abort.

.flowControl('serial'[, key])

Wait for the previous request to complete before running when it hasn't finished.

import { request } from 'keq'


await Promise.all([1,2,3].map(async i => {
  await request
    .get('/cat')
    .flowControl('serial', 'cat-request')
})
tip

A web page in a browser can send a maximum of 6 HTTP requests simultaneously. Exceeding requests will wait for previous requests to complete before being sent. serial mode ensures that only one HTTP connection is occupied by a key at a time.

.flowControl('abort'[, key])

When the previous HTTP request hasn't finished, abort it and immediately send this HTTP request.

import { request } from "keq"

await Promise.all(
  [1, 2, 3].map(async (i) => {
    await request
      .get("/cat")
      .flowControl("abort", "cat-request")
  })
)
tip

In frameworks like React/Vue, when component rendering is triggered multiple times consecutively, immediately aborting the previous HTTP request can avoid long-running requests occupying HTTP resources for extended periods.