Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSteve Seebart
Courses Plus Student 11,897 PointsMoving error handling out of the promise
If I had multiple network request promises in my js file and the same block of error handling code for each, is there some way to move
xhr.onerror = () => reject(Error('A network error occurred.'));
into a reusable function?
Trying to find a way that would keep me from repeating the same code in each promise.
1 Answer
Steven Parker
231,184 PointsWhat if you created a function that returns a function that calls "reject" with the desired argument:
const rejector = reject => () => reject(Error('A network error occurred.'));
And then called it when needed to set up an error handler:
xhr.onerror = rejector(reject);