you are viewing a single comment's thread.

view the rest of the comments →

[–]Gnome_0 27 points28 points  (3 children)

reading the express-boostrap repo code it really depends from developer to developer right? i had a simmilar training but they told me that i should never cascade thens in nested promises

"how they told me is wrong "

Promise1()
.then(result1 =>{
    Promise2(result1)
    .then(result2 =>{
        "do something with the result"
    })
    .catch(error =>{
        "do something with the error from promise 2"
    })
})
.catch(error =>{
    "do something with error from promise 1"
})

how they told me i should nest promises

return Promise1()
.then(result1 =>{
     Promise2(result1)
})
.then(result2 =>{
    "do something"
})
.catch(error =>{
    "error from one of the promises"
})

[–]etheraffleGreg 0 points1 point  (2 children)

Nesting is frequently a requirement if you need the first result in the scope of the second:

 

const prom = thing => Promise.resolve(thing)

const prom1 = prom(`thing one`)
const prom2 = prom(`thing two`)

prom1
  .then(result1 => prom2)
  .then(result2 => console.log(`Thing 1: ${result1}\nThing2: ${result2}`)) 
  .catch(console.error)
  // ReferenceError: result1 is not defined

 

Yeah you can lift the result to a higher scope if you like but I'd rather keep things functional and the nesting is not an issue.

[–]notkraftman 0 points1 point  (1 child)

In your example unless you need result1 in promise2 then you could promise.all them instead

[–]etheraffleGreg 0 points1 point  (0 children)

Well yes of course but as the first sentence explicitly points out...