Skip to content

Instantly share code, notes, and snippets.

@gvost
Last active January 10, 2020 20:39
Show Gist options
  • Save gvost/fabbb3cada752203fa76b7b103b051c0 to your computer and use it in GitHub Desktop.
Save gvost/fabbb3cada752203fa76b7b103b051c0 to your computer and use it in GitHub Desktop.
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