Skip to content

Instantly share code, notes, and snippets.

@poeticninja
Created May 10, 2017 20:00
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 poeticninja/2162a4c22aa67e87ab5ac2fb2a09edd0 to your computer and use it in GitHub Desktop.
Save poeticninja/2162a4c22aa67e87ab5ac2fb2a09edd0 to your computer and use it in GitHub Desktop.
Example serial promises with error to handle individual cases.
const delay = require("delay");
const people = [
{
id: 0,
name: "Stewie",
delay: 200
},
{
id: 1,
name: "Lois",
delay: 100
},
{
id: 2,
name: "Peter",
delay: 400
},
{
id: 3,
name: "Brian",
delay: 10
},
{
id: 4,
name: "Glen",
delay: 300
}
];
people.reduce((promise, person, index, people) => {
return promise.then(() => {
return delay(person.delay).then(() => {
if (person.id === 2) {
// This is to simulate an error half way through.
throw new Error("what up my foool!");
}
console.log("Look who we found!");
console.log(person.id, person.name, person.delay);
}).catch((err) => {
// Here you handle each individual error. it does not stop the chain.
console.log(err);
});
});
}, Promise.resolve()).then(() => {
console.log("WE GOT DONE BISH!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment