Skip to content

Instantly share code, notes, and snippets.

@Gaafar
Last active June 18, 2019 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Gaafar/de1266d2338ba2393e5bada866178067 to your computer and use it in GitHub Desktop.
Save Gaafar/de1266d2338ba2393e5bada866178067 to your computer and use it in GitHub Desktop.
const makeRequest = () => {
return getJSON()
.then(data => {
if (data.needsAnotherRequest) {
return makeAnotherRequest(data)
.then(moreData => {
console.log(moreData)
return moreData
})
} else {
console.log(data)
return data
}
})
}
@sanyatuning
Copy link

const makeRequest = () => {
return getJSON()
.then(data => {
if (data.needsAnotherRequest) {
return makeAnotherRequest(data)
} else {
return data
}
}).then(data => {
console.log(data)
})
}

@nick-nyllabs
Copy link

@justsml
Copy link

justsml commented Jun 26, 2017

Nice @sanyatuning +1 ... I'd shorten it up a little more:

const makeRequest = () => {
  return getJSON()
  .then(data => data.needsAnotherRequest
      ? makeAnotherRequest(data)
      : data)
  .then(data => console.log(data) || data) 
}

@Sufiane
Copy link

Sufiane commented Jul 12, 2017

+1 for both of you

@optimistanoop
Copy link

Removing one label of nesting of nesting

const makeRequest = () => {
  return getJSON()
      .then(data => {    
        if (data.needsAnotherRequest) {
          return makeAnotherRequest(data)
         }else {
           return data
         }
       })
       .then(moreData => {
         return moreData
       })
  } 

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