1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { fetch } from "https://esm.town/v/std/fetch";
export const test_concurrencyDoesntWork = (async () => {
const fetchDelayedResponse = async (index: number) => {
const start = Date.now();
const response = await fetch(
"https://api.val.town/v1/express/alp.proxyFetch1",
{
method: "POST",
body: JSON.stringify({
url: "https://hub.dummyapis.com/delay?seconds=1",
}),
}
);
console.log(
`Returned data for ${index}, took:`,
(Date.now() - start) / 1000,
"seconds"
);
};
const fetchDelayedResponseWithLoadBalancer = async (index: number) => {
const start = Date.now();
const response = await fetch(
`https://api.val.town/v1/express/alp.proxyFetch${index}`,
{
method: "POST",
body: JSON.stringify({
url: "https://hub.dummyapis.com/delay?seconds=2",
}),
}
);
console.log(
`Returned data for ${index}, took:`,
(Date.now() - start) / 1000,
"seconds"
);
};
console.log("w/o load balancer");
await Promise.all([
fetchDelayedResponse(1),
fetchDelayedResponse(2),
fetchDelayedResponse(3),
fetchDelayedResponse(4),
fetchDelayedResponse(5),
]);
console.log("w/ load balancer");
await Promise.all([
fetchDelayedResponseWithLoadBalancer(1),
fetchDelayedResponseWithLoadBalancer(2),
fetchDelayedResponseWithLoadBalancer(3),
fetchDelayedResponseWithLoadBalancer(4),
fetchDelayedResponseWithLoadBalancer(5),
]);
})();
๐Ÿ‘† This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data โ€” all from the browser.