-
-
Save dmamills/288f1b839c172fe35561e087e2dd14e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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