File size: 1,409 Bytes
3b26db5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Log when the worker is loaded to confirm it's running
console.log('Worker script loaded');

// Function to handle messages sent to the worker
onmessage = function(event) {
    console.log('Worker received message:', event.data); // Log when the worker receives a message

    if (event.data === 'fetch') {
        console.log('Worker is now fetching data...');

        // Fetch data from the server
        fetch('/getguessednamepath/')
            .then((response) => {
                if (!response.ok) {
                    console.error('Network response was not ok');
                    throw new Error('Network response was not ok');
                }
                return response.json(); // Parse the response as JSON
            })
            .then((data) => {
                console.log('Data fetched by worker:', data); // Log the fetched data
                postMessage({ status: 'success', data }); // Send the fetched data back to the main thread
            })
            .catch((error) => {
                console.error('Error fetching data in worker:', error); // Log any errors in the worker
                postMessage({ status: 'error', error: error.message }); // Send error message back to the main thread
            });
    } else {
        console.log('Unknown message received by worker:', event.data); // Log any unknown messages
    }
};