相关疑难解决方法(0)

如何使用Cloudflare Worker创建异步请求(非阻塞)

我正在编写一个Cloudflare Worker,需要在我的原始请求完成后ping一个分析服务.我不希望它阻止原始请求,因为我不希望延迟或分析系统的故障减慢或中断请求.如何创建在原始请求完成后开始和结束的请求?

addEventListener('fetch', event => {
  event.respondWith(handle(event))
})

async function handle(event) {
  const response = await fetch(event.request)

  // Send async analytics request.
  let promise = fetch("https://example.com")
      .then(response => {
    console.log("analytics sent!")
  })

  // If I uncomment this, only then do I see the
  // "analytics sent!" log message. But I don't
  // want to wait for it!
  //  await promise;

  return response
}
Run Code Online (Sandbox Code Playgroud)

javascript cloudflare-workers

5
推荐指数
1
解决办法
307
查看次数

标签 统计

cloudflare-workers ×1

javascript ×1