Created
August 22, 2018 23:18
-
-
Save alfredayibonte/078c3705f4fd0f5a11c1b0f09fa9ff5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let count = 0; | |
self.addEventListener('message', e => { | |
console.log('web worker is starting...') | |
let { requestUrls } = e.data; | |
const req = () => { | |
fireRequest(requestUrls[count], token) | |
.then((data) => { | |
//posts to component to be store in db. | |
postMessage({ data, key: requestUrls[count] }) | |
if( count < requestUrls.length -1) { | |
count = count + 1; | |
req(); | |
} | |
else { | |
//post message to component when done | |
postMessage({ status: 'done' }) | |
} | |
}) | |
.catch((err) => console.log('err:', err)) | |
} | |
req(); | |
}) | |
function fireRequest(url, token) { | |
return fetch(url, { | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}) | |
.then(res => res.json()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment