Created
May 10, 2017 20:00
-
-
Save poeticninja/2162a4c22aa67e87ab5ac2fb2a09edd0 to your computer and use it in GitHub Desktop.
Example serial promises with error to handle individual cases.
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
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