Class 35 Flashcards
What would happen in this code?
function houseOne(){
console.log(‘Paper delivered to house 1’)
}
function houseTwo(){
console.log(‘Paper delivered to house 2’)
}
function houseThree(){
console.log(‘Paper delivered to house 3’)
}
houseOne()
houseTwo()
houseThree()
Paper delivered to house 1
Paper delivered to house 2
Paper delivered to house 3
What would happen in this code?
function houseOne(){
console.log(‘Paper delivered to house 1’)
}
function houseTwo(){
setTimeout(() => console.log(‘Paper delivered to house 2’), 3000)
}
function houseThree(){
console.log(‘Paper delivered to house 3’)
}
houseOne()
houseTwo()
houseThree()
Paper delivered to house 1
Paper delivered to house 3
Paper delivered to house 2
What is a callback?
A function that has been passed as an argument
What is a higher order function?
A function that takes another function as an argument
What would happen in this code?
function houseOne(){
console.log(‘Paper delivered to house 1’)
}
function houseTwo(callback){
setTimeout(() => {
console.log(‘Paper delivered to house 2’)
callback()
}, 3000)
}
function houseThree(){
console.log(‘Paper delivered to house 3’)
}
houseOne()
houseTwo(houseThree)
Paper delivered to house 1
(After 3 seconds)
Paper delivered to house 2
Paper delivered to house 3
When does callback fire?
When async task or another function is done
(We waited for the set timeout to finish, then the callback fires right after)
What would happen in this code?
function houseOne(){
setTimeout(() => {
console.log(‘Paper delivered to house 1’)
setTimeout(() => {
console.log(‘Paper delivered to house 2’)
setTimeout(() => {
console.log(‘Paper delivered to house 3’)
}, 3000)
}, 4000)
}, 5000)
}
houseOne()
(After 5 seconds)
Paper delivered to house 1
(After 4 seconds)
Paper delivered to house 2
(After 3 seconds)
Paper delivered to house 3
What is a promise?
A promise is an object that represents the eventual completion or failure of an async operation and its value;
An object that may have a value in the future
What are 3 possible states a promise can have?
- pending: initial state (neither fulfilled nor rejected)
- fulfilled: operation was completed successfully
- rejected: operation failed
What is .then() ?
A promise object METHOD that runs after promise “resolves”
What is .then(value)?
Whatever value the promise objet has gets passed as an argument
How does fetch API relate to promise?
Fetch API returns promise
What is await?
Await waits for an async process to complete inside an Async function
For backend development, what do servers need?
Disk access (hard drive/ssd) and network access (internet request / responses)
Servers serve up files (files are stored in disk, hard drive or ssd)
Servers need to hear a request for those files and respond with those files (Servers need access to the internet)
Explain about js and its single-threaded and synchronous nature
JS is single threaded and synchronous, meaning it can only do one task at a time. In order for program to continue with flow (without any blocking and stopping), JS will hand off a lot of asynchronous tasks to the browser (JS runs in the browser environment). Eventually, web APIs from the browser will respond back and JS need to handle those responses. JS deals with responses with callbacks, promises, and async/await