轮询

在后续请求中产生不同的响应。

你可以在 JavaScript 中使用 生成器函数(包括异步生成器),在对同一处理程序的每个后续请求中生成不同的响应。这对于描述 HTTP 轮询特别方便。

¥You can use generator functions (including async generators) in JavaScript to yield different responses on each subsequent request to the same handler. This is particularly handy to describe HTTP polling.

http.get<{ city: string }, never, { degree: number }>(
  '/weather/:city',
  function* () {
    let degree = 25
 
    while (degree < 27) {
      degree++
      yield HttpResponse({ degree })
    }
 
    degree++
    return HttpResponse.json({ degree })
  },
)

GET /wheather/:city 请求处理程序会在每次响应时递增 degree,直到达到 28 度:

¥This GET /wheather/:city request handler increments the degree on each response until it reaches 28 degrees:

GET /weather/london // { "degree": 26 }
GET /weather/london // { "degree": 27 }
GET /weather/london // { "degree": 28 }
 
// All subsequent requests will respond
// with the latest returned response.
GET /weather/london // { "degree": 28 }