Last active
January 10, 2020 20:39
-
-
Save gvost/fabbb3cada752203fa76b7b103b051c0 to your computer and use it in GitHub Desktop.
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
function sortObj(rawObject) { | |
let newArr = [] | |
return new Promise(fulfill, reject) { | |
for (let i = 0; i < rawObject.meals.length; i++) { | |
newArr.push({ | |
meal: obj.meals[i].strMeal, | |
instructions: obj.meals[i].strInstructions, | |
thumb: obj.meals[i].strMealThumb, | |
ingredients: [], | |
measurements: [] | |
}) | |
} | |
if (newArr.length !== obj.meals.length) { | |
setTimeout(() => { | |
fulfill(newArr) | |
console.log('new data', newArr) | |
}, 300) | |
} else { | |
fulfill(newArr) | |
} | |
} | |
} | |
function addIngredients(newDataArray) { | |
return new Promise(fulfill, reject) { | |
for (let i = 0; i < responseJson.meals.length; i++) { | |
for (let a = 1; a < 21; a++) { | |
if (responseJson.meals[i]["strIngredient" + a] !== null && responseJson.meals[i]["strIngredient" + a] !== "") { | |
newDataArray[i].ingredients.push(data.meals[i]["strIngredient" + a]) | |
newDataArray[i].measurements.push(data.meals[i]["strMeasure" + a]) | |
} | |
} | |
} | |
setTimeout(() => { | |
fulfill(newDataArray) | |
}, 800) | |
} | |
} | |
function createDomElements(newDataArray) { | |
for (let i = 0; i < newDataArray.length; i++) { | |
$('.results').append( | |
// your DOM elements | |
); | |
} | |
} | |
function displayRecipeResults(responseJson) { | |
if (responseJson.meals.length !== 0) { | |
sortObj(responseJson).then(resultingObject => { | |
// example: | |
// resultingObject = [{ | |
// meal: obj.meals[i].strMeal, | |
// instructions: obj.meals[i].strInstructions, | |
// thumb: obj.meals[i].strMealThumb, | |
// ingredients: [], | |
// measurements: [] | |
// }, {}, {}] | |
addIngredients(resultingObject).then(finalResult => { | |
createDomElements(finalResult) | |
}) | |
}) | |
} else { | |
// message user | |
$('.results').append(`No recipes found by that name. Try again.`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment