Skip to content

Instantly share code, notes, and snippets.

@Couto
Created September 17, 2015 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Couto/4a3bfc03726832127d1c to your computer and use it in GitHub Desktop.
Save Couto/4a3bfc03726832127d1c to your computer and use it in GitHub Desktop.
// 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