我正在寻找一个示例,说明这两行代码在正常运行的 A/B 测试工作人员中是什么样子。来自 https://developers.cloudflare.com/workers/examples/ab-testing
const TEST_RESPONSE = new Response("Test group") // e.g. await fetch("/test/sompath", request)
const CONTROL_RESPONSE = new Response("Control group") // e.g. await fetch("/control/sompath", request)
我使用了示例,替换了我正在使用的路径,并收到语法错误,指出等待只能在异步中使用。所以我将函数更改为异步函数handleRequest(request)并得到了500错误。
这两行代码应该是什么样子才能工作?
最佳答案
工作示例
document.getElementById("myBtn").addEventListener("click", myFun);
async function myFun() {
const isTest = Math.random() > 0.5 //here you place your condition
const res = isTest ? await fetch1() : await fetch1() //here you should have different fetches for A and B
console.log(res)
document.getElementById("demo").innerHTML = res.message;
}
async function fetch1() {
//I took the link from some other SO answer as I was running into CORS problems with e.g. google.com
return fetch("https://currency-converter5.p.rapidapi.com/currency/list?format=json", {
"method": "GET",
"headers": {
"x-rapidapi-host": "currency-converter5.p.rapidapi.com",
"x-rapidapi-key": "**redacted**"
}
})
.then(response => response.json())
}
<button id="myBtn">button</button>
<div id="demo">demo</div>
<script src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script>
<script type="text/babel">
</script>
关于javascript - 如何与 Cloudflare 工作人员进行 A/B 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68574677/