-
-
Save magalhini/da879965cba741dbf2de 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
// Kinda pseudo code | |
// This script will download and resize each image to half | |
// Because the images can be RRREAAALLY big | |
// the download and resize is done sequentially (in my case, inside a child_process) | |
// to ensure that memory is released after each image. | |
// We dont know how many images there are | |
let images = [ | |
'https://flickr.com/photo/1', | |
'https://flickr.com/photo/2' | |
'https://flickr.com/photo/3' | |
'https://flickr.com/photo/4' | |
]; | |
// Fetch and resize (it usually forks a new chil process to do this); | |
let resize = (image) => { | |
return () => { | |
return fetch(url).then((image) => { | |
image.width = image.width / 2; | |
image.width = image.width / 2; | |
return image; | |
}); | |
}; | |
}; | |
// Chain the resize functions into a sequential promise | |
// it should only fetch one image after it has completed the last one | |
let process = images.reduce((promise, image) => { | |
return promise.then(resize(image)); | |
}, Promise.resolve(true)); // Create an empty promise | |
process.then(() => { | |
// All images should have been downloaded and resized | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment