DEV Community

Discussion on: How to rewrite a callback function in Promise form and async/await form in JavaScript

Collapse
 
itachiuchiha profile image
Itachi Uchiha

I remember this picture

https://cdn-images-1.medium.com/max/823/1*Co0gr64Uo5kSg89ukFD2dw.jpeg

I don't know if there a promise hell example. Thanks for this great article.

Collapse
 
ccleary00 profile image
Corey Cleary • Edited

Haha yeah this one's classic - good point too, you can hit "promise hell" too if you're not careful. Not as "pyramid" like as callback hell, but having ton's and tons of then's can get sort of annoying

Collapse
 
l2aelba profile image
l2aelba

Think also if it's also with If-Else Statements :)

Collapse
 
quantumsheep profile image
Nathanael Demacon

Promise hell can't exist, that's why it's so nice!

const call_api = (url) => fetch(url).then(res => res.json())
const heaven = (url) => call_api(url).then(data => console.log(data))

heaven('/api/users')
  .then(() => heaven('/api/posts'))
  .then(([{ id }]) => heaven(`/api/posts/reactions?post=${id}`))
  .then(({ post: id }) => heaven(`/api/comments?post=${id}`))

See? It's really clean 😃