DEV Community

Discussion on: Notes on ECMAScript 6 (ES6)

Collapse
ikirker profile image
Ian Kirker

As someone who tries to write as little JavaScript as possible, I'm not sure I'm looking forward to infinite chains of impossible-to-debug promises any more than I currently enjoy infinite chains of impossible-to-debug callbacks.

Collapse
hardy613 profile image
Scott Hardy Author • Edited on

Hey Ian,

Thanks for taking time to leave a comment, what part of a Promise Chain are you finding impossible to debug? Promises offer a second failed function within a then or the use of a catch to manage errors. Maybe I can help clear up some confusion.

In my opinion a promise chain is relief from callback hell.

Collapse
ikirker profile image
Ian Kirker

I've not actually tried using them yet, but my comment mostly comes from looking at that chain, imagining a longer one, and then imagining trying to work out which .then and .catch happen at which level, much like trying to work out which level of }) } } }) ) }, }; } } }) the problem is in with callback hell.

I guess it should at least be easier to add good error reporting in the .catch blocks.

Thread Thread
hardy613 profile image
Scott Hardy Author

I recommend trying promises out, start by working with promises before creating them. A promise that you could work with is the fetch api for example, google has a good introduction

const getJson = res => {
    // comment out the if to return the error 
    // and see how catch works
    if(res.status !== 200) {
        return new Error(`StatusCode: ${res.status}`)
    }
    return res.json()
}
const getUrl = url => fetch(url).then(getJson)

getUrl('https://baconipsum.com/api/?type=meat-and-filler')
    .then(res => console.log(res))
    .catch(err => console.error(err.message))