Skip to content

Instantly share code, notes, and snippets.

@raycohen
Created September 18, 2017 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raycohen/f3d33314f710802a5e0a0575f9147fba to your computer and use it in GitHub Desktop.
Save raycohen/f3d33314f710802a5e0a0575f9147fba to your computer and use it in GitHub Desktop.
Weichu debug solution
/**
* This file represents a simple application where we make 10 api requests to get
* 10 random words back and then append them based on the index of the word at the time the request was made
* There are many things wrong with this file, but when all are fixed the application should log out the list of words (generator.result)
*/
var TOTAL_WORDS = 10;
var resultDiv = document.getElementById('result');
console.log(resultDiv);
resultDiv.innerHTML = "Result pending..."
//
// params: times, how many times you want it get generated
// return: Array of generated words
//
function WordGenerator(times) {
var promiseList = [];
for (var i = 0; i < times; i++) {
promiseList.push(fetch('http://api.wordnik.com/v4/words.json/randomWord?api_key=4e335efc337da66cce00203640a08660179430ac607ba303c')
.then((response) => {
return response.json();
}).then((jsonData) => {
return jsonData.word;
}))
}
return Promise.all(promiseList)
.then(values => {
// console.log('promiseall', values);
return values;
})
}
function run() {
// call WordGenerator
WordGenerator(3).then((res)=> console.log('random', res));
// style it ...
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment