Skip to content

Instantly share code, notes, and snippets.

@alfredayibonte
Created August 22, 2018 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfredayibonte/078c3705f4fd0f5a11c1b0f09fa9ff5f to your computer and use it in GitHub Desktop.
Save alfredayibonte/078c3705f4fd0f5a11c1b0f09fa9ff5f to your computer and use it in GitHub Desktop.
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