Skip to content

Instantly share code, notes, and snippets.

@leifoolsen
Last active June 16, 2020 08:19
Show Gist options
  • Save leifoolsen/c03a6299c2423b83413526aec8f91b87 to your computer and use it in GitHub Desktop.
Save leifoolsen/c03a6299c2423b83413526aec8f91b87 to your computer and use it in GitHub Desktop.
// ------------------------------------------------------------
// Datamodell
// ------------------------------------------------------------
const fruitBasket = {
apple: 27,
banana: 1,
grape: 22,
pear: 14,
};
// Requestemulator
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const fetchFruits = async (fruit) => {
const delay = fruit === 'grape' ? 100 : 1000;
return sleep(delay).then(() => {
const numFruits = fruitBasket[fruit];
if (numFruits < 1) {
throw new Error(`No ${fruit}`);
}
return `${fruit}: ${numFruits}`;
});
};
const fruitsToFetch = ['apple', 'banana', 'grape', 'pear'];
// ------------------------------------------------------------
// Sekvensiell kjøring
// ------------------------------------------------------------
const forLoop = async () => {
const t = new Date();
console.log('Start')
try {
for (let index = 0; index < fruitsToFetch.length; index++) {
const fruit = await fruitsToFetch[index];
const numFruits = await fetchFruits(fruit);
console.log(numFruits);
}
}
catch (err) {
console.log('ERROR:', err.message);
}
console.log('End', new Date() - t, 'ms');
};
// forLoop();
// ------------------------------------------------------------
// Parallell kjøring. Promise.all === fail fast
// ------------------------------------------------------------
const promiseAllLoop = async () => {
const t = new Date();
console.log('Start');
const promises = fruitsToFetch.map(async (fruit) => {
const numFruit = await fetchFruits(fruit);
return numFruit;
})
try {
const numFruits = await Promise.all(promises);
console.log(numFruits);
}
catch (err) {
console.log('ERROR:', err.message);
}
console.log(promises);
console.log('End', new Date() - t, 'ms');
};
// promiseAllLoop();
// ------------------------------------------------------------
// Parallell kjøring. Promise.allSettled
//
// "The Promise.allSettled() method returns a promise
// that resolves after all of the given promises have
// either fulfilled or rejected, with an array of
// objects that each describes the outcome of each promise."
//
// Med Promise.allSettled() sikrer vi at alle taskene kjøres,
// mens Promise.all() bryter så snart en av taskene feiler.
// ------------------------------------------------------------
const allSettledLoop = async () => {
const t = new Date();
console.log('Start');
const promises = fruitsToFetch.map(async (fruit) => {
const numFruit = await fetchFruits(fruit);
return numFruit;
})
await Promise.allSettled(promises).then((results) => {
results.forEach((result) => {
console.log(result.status, result.value);
});
});
console.log('Promises:', promises);
console.log('End', new Date() - t, 'ms');
};
// allSettledLoop();
// ------------------------------------------------------------
// Parallell kjøring. Promise.race === The winner takes it all
// ------------------------------------------------------------
const raceLoop = async () => {
const t = new Date();
console.log('Start');
const promises = fruitsToFetch.map(async (fruit) => {
const numFruit = await fetchFruits(fruit);
return numFruit;
})
await Promise.race(promises).then((value) => {
console.log(value);
});
console.log('End', new Date() - t, 'ms');
};
// raceLoop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment