Last active
August 15, 2016 09:18
-
-
Save thorstenweber83/5175be3b97c962e2488ef08d2c6f4bc6 to your computer and use it in GitHub Desktop.
Exercise 37: Sequencing HTTP requests with Observable: async await version
This file contains 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
// this is an implementation of exercise 37 of | |
// http://reactivex.io/learnrx/ | |
// to compare with the Observable implementation | |
// i assume that getJSON returns a Promise instead of an Observable... | |
function load(){ | |
return new Promise(function(resolve,reject){ | |
window.onload = resolve; | |
}); | |
} | |
function(window, getJSON, showMovieLists, showError) { | |
async function processMovieLists(){ | |
try{ | |
const abTestInformation = await getJSON("http://api-global.netflix.com/abTestInformation"); | |
const results = await Promise.all([ | |
getJSON("http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/config"), | |
getJSON("http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/movieLists"), | |
]); | |
const config = results[0]; | |
const movieListsMessage = results[1]; | |
const copyOfMovieLists = Object.create(movieListsMessage.list); | |
if(config.showInstantQueue){ | |
const queueMessage = await getJSON("http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/queue"); | |
const queue = queueMessage.list; | |
copyOfMovieLists.push(queue); | |
} | |
await load(); | |
showMovieLists(copyOfMovieLists); | |
} | |
catch(err){ | |
showError(err); | |
} | |
} | |
processMovieLists(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment