Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Mar 27, 2017

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

@nick-nyllabs

This comment has been minimized.

Copy link

commented Mar 30, 2017

@justsml

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jul 12, 2017

+1 for both of you

@optimistanoop

This comment has been minimized.

Copy link

commented Jul 25, 2018

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
You can’t perform that action at this time.