Skip to content

Instantly share code, notes, and snippets.

@dmamills
Forked from Gaafar/if-promise.js
Last active March 27, 2017 18:27
Show Gist options
  • Save dmamills/288f1b839c172fe35561e087e2dd14e2 to your computer and use it in GitHub Desktop.
Save dmamills/288f1b839c172fe35561e087e2dd14e2 to your computer and use it in GitHub Desktop.
//Original
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
}
})
}
//Async
const makeRequest = async () => {
const data = await getJSON()
if (data.needsAnotherRequest) {
const moreData = await makeAnotherRequest(data);
console.log(moreData)
return moreData
} else {
console.log(data)
return data
}
}
//Mills:
const onResult = (data) => {
return new Promise((resolve, reject) => {
if(data.needsAnotherRequest) {
makeAnotherRequest(resolve,reject);
} else {
resolve(data)
}
}
}
const makeRequest = () => {
return getJSON()
.then(onResult)
.then(data => {
console.log(data);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment