Fork and clone this lab. Respond to questions in clear, concise sentences directly within this markdown file.
1. What will the following code snippet log? Why?
console.log('Line 1')
setTimeout(() => console.log('Line 2'), 1000)
console.log('Line 3')
2. What does the following code snippet log? Why?
function createPromise(seconds) {
return new Promise((resolve) => {
setTimeout(function() {
resolve(`After ${seconds} second(s), this promise is resolved.`)
}, seconds * 1000)
})
}
console.log(createPromise(1000))
3. How do we use our createPromise
function to log "After 1 second(s), this promise is resolved."
4. What does the following code snippet return? What does it log?
const ouPromise = new Promise((resolve) => {
resolve(12);
})
ourPromise.then(value => value * 2);
5. What does the last line of code return? What does it log?
Note: Instead of using the Promise
constructor to create a Promise that immediately resolves to 12, we can just use the Promise.resolve
method.
const ourPromise = Promise.resolve(12);
ourPromise.then(value => value * 2).then(value => value + 10);
6. What does the following code snippet return? What does it log? How does this differ from the question above?
Promise.resolve(12).then(value => value * 2).then(value => console.log(value + 10))
7. What does the following code snippet return? What does it log? How does this differ from the question above?
Promise.resolve(12).then(value => value * 2).then(value => {
console.log(value + 10);
return value + 10;
});
8. What does the following code snippet return? What does it log? How does this differ from the question above?
Promise.reject(12).then(value => value * 2).then(value => {
console.log(value + 10);
return value + 10;
}).catch(reason => {
const message = `${reason} is a bad number.`;
console.error(message);
return reason;
});