Do Now: Pull up last weeks activity and attempt some of the later parts: here
Run line by line
addEventListener- schedules code to be executed later, typically on an action. It doesn't get run right away!
This is called a callback function
decoding .WAV file data with the Web Audio API
setTimeout
After 1000 milliseconds, the code we schedule runs
Call me back in 10 minutes after you've poked around some callback examples!
Tim's codepenWhen async code depends on prior asynchronous code... it gets messy
The last line was about the very first callback delay
Super nested
Promise chaining
When a promise resolves we can call .then() on it to run our callback
function countDown() {
console.log("three...");
return sleep(1000).then(() => {
console.log("two...");
return sleep(1000);
}).then(() => {
console.log("one...");
return sleep(1000);
});
}
countDown().then(() => {
console.log("Happy New Year!");
});
async function countDown() {
console.log("three...");
await sleep(1000);
console.log("two...");
await sleep(1000);
console.log("one...");
await sleep(1000);
}
countDown().then(() => {
console.log("Happy New Year!");
});
Sometimes the result of a promise is an error :(
They can occur due to various reasons like invalid input, network issues, or unexpected behavior.
We don't want to crash our program if there's an error.
Multiple lines potentially cause an error...
Other resources: