Skip to content

Instantly share code, notes, and snippets.

@paulbreslin
Last active May 22, 2018 22:31
Show Gist options
  • Save paulbreslin/2cbd49e98e53eafba2ab8af385646e90 to your computer and use it in GitHub Desktop.
Save paulbreslin/2cbd49e98e53eafba2ab8af385646e90 to your computer and use it in GitHub Desktop.
const example = require('example-library');
example.firstAsyncRequest()
.then( fistResponse => {
example.secondAsyncRequest(fistResponse)
.then( secondResponse => {
example.thirdAsyncRequest(secondResponse)
.then( thirdAsyncResponse => {
// Insanity continues
});
});
});
@Sufiane
Copy link

Sufiane commented Jul 13, 2017

You could do it this way:

example. firstAsyncRequest()
   .then(firstResponse => {
      return example.secondAsyncRequest(firstResponse)
   })
  .then(secondResponse => {
    return example.thirdAsyncRequest(secondResponse)
  })
  .then(thirdResponse => {
     // you can keep coding with sanity :)
  })

Promise are here to resolve the callback hell, no need to do it again with them ahah

@JimiHFord
Copy link

Or even:

example.firstAsyncRequest()
  .then(firstResponse => example.secondAsyncRequest(firstResponse))
  .then(secondResponse => example.thirdAsyncRequest(secondResponse))
  .then(thirdResponse => {
     // you can keep coding with sanity :)
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment