scrimba
Frontend Career Path
Working with APIs
Async JS
Thought experiment: what if `fetch` used callbacks?
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Thought experiment: what if `fetch` used callbacks?
AboutCommentsNotes
Thought experiment: what if `fetch` used callbacks?
Expand for more info
index.js
run
preview
console
function handleClick() {
fetch("https://apis.scrimba.com/deckofcards/api/deck/new/shuffle/")
.then(res => res.json())
.then(data => console.log(data))
}

document.getElementById("new-deck").addEventListener("click", handleClick)

// function callback() {
// console.log("I finally ran!")
// }

// setTimeout(callback, 2000)

// const people = [
// { name: "Jack", hasPet: true },
// { name: "Jill", hasPet: false },
// { name: "Alice", hasPet: true },
// { name: "Bob", hasPet: false },
// ]

// function gimmeThePets(number) {
// return person.hasPet
// }

// const peopleWithPets = people.filter(gimmeThePets)
// console.log(peopleWithPets)

// const people = [
// { name: "Jack", hasPet: true },
// { name: "Jill", hasPet: false },
// { name: "Alice", hasPet: true },
// { name: "Bob", hasPet: false },
// ]

// function filterArray(array, callback) {
// const resultingArray = []
// // Write your filtering logic here
// for (let item of array) {
// const shouldBeIncluded = callback(item)
// if (shouldBeIncluded) {
// resultingArray.push(item)
// }
// }
// return resultingArray
// }

/**
* Challenge: Use your filter array method!
* Given the above `people` array, return a new array with only people where `hasPet` is true
* Note: Remember that your callback function will be given the individual item in the array for a parameter
*/

// const peopleWithPets = filterArray(people, function(person) {
// return person.hasPet
// })

// console.log(peopleWithPets)
Console
[
{name:
"Jack"
, hasPet:
true
}
,
{name:
"Alice"
, hasPet:
true
}
]
,
{success:
true
, deck_id:
"e4n850ypjaxt"
, remaining:
52
, shuffled:
true
}
,
/index.html
-2:20