Questions
1. 아래 예제 끝 console.log(todos)에서 undefined가 출력되는 이유는?
let todos;
const get = url => {
const xhr = new XMLHttpRequest();
xhr.open(’GET’, url);
xhr.send();
xhr.onload = () => {
if(xhr.status === 200) {
todos = JSON.parse(xhr.response);
} else {
console.error(xhr.status);
}
}
}
get('https://jsonplaceholder.typicode.com/posts/1');
console.log(todos); //2. undefined
2. 아래 예제에서 2 - 3 - 1로 출력되는 이유는 프로미스 후속 처리 메서드의 콜백 함수는 태스크 큐가 아니라 ? 에 저장되기 때문이다. ? 에 들어가는 단어는?
setTimeout(() = > console.log(1), 0);
Promise.resolve()
.then(() = > console.log(2))
.then(() = > console.log(3));
//2
//3
//1
3. 아래 코드의 단점은?
promiseGet('https://...').then(
res = > console.xxx(res);,
err = > console.error(err)
);