rand-client.html 938 Bytes
Newer Older
Caleb C. Sander's avatar
Caleb C. Sander committed
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
<html>
  <head>
    <script>
      // Make a POST request to the given URL,
      // using the given JSON string as the body
      fetch('http://localhost/seed', {
        method: 'POST',
        body: JSON.stringify({seed: Date.now()})
      })
        .then(res => {
          const promises = []
          // Get 100 random numbers
          for (let i = 0; i < 100; i++) {
            promises.push(
              // Make a GET request to the given URL
              fetch('http://localhost/next')
                // Parse the response from JSON
                .then(res => res.json())
            )
          }
          // Wait for all 100 responses
          return Promise.all(promises)
        })
        .then(numbers => {
          // Extract the value from each JSON response
          // and print it to the console
          for (const number of numbers) console.log(number.value)
        })
    </script>
  </head>
</html>