댓글 검색 목록

[Nodejs] JavaScript Promises : 9 가지 질문

페이지 정보

작성자 운영자 작성일 19-12-20 18:06 조회 523 댓글 0

약속에 대해 배우고 퀴즈를 풀어보세요!


https://danlevy.net/javascript-promises-quiz/ 


JavaScript 및 약속을 알고 있습니까 ???

  • JavaScript 기술을 입증하십시오! ?
  • 로그인이나 가입이 필요하지 않습니다. ✨
  • 다중 선택. ?… 얼마나 어려울 수 있습니까?

목표 


내 목표는 내 블로그를 위해 만든 새로운 '퀴즈 툴'을 사용하고 운동에서 얼마나 잘 배웠는지에 대한 피드백을 얻는 것입니다.

  1. 힌트를 읽으십시오 (녹색 버튼, 왼쪽 하단). 일부에는 자세한 주석이 포함 된 몇 가지 애니메이션 답변이 포함되어 있으며 일부는 단서 만 제공합니다. 모든 답변을 너무 쉽게 줄 수는 없습니다! 진지하게, 이것은 의도적이며, 대답이 있습니다. 건너 뛰거나 코드 스니펫을 사용 해보고 변경 한 다음 더 재생하십시오.
  2. 브라우저의 콘솔에서 코드를 사용해 보거나 (바로 가기 F12를 시도하거나 검색) repl.it *를 사용하십시오.
  3. 저에게 @justsml ​​트윗을 주시기 바랍니다. 당신의 생각을 듣고 싶습니다!

? 아래 9 가지 질문을 완료하십시오 


1. Multiple .catch’s 


var p = new Promise((resolve, reject) => {
  reject(Error('The Fails!'))
})
p.catch(error => console.log(error.message))
p.catch(error => console.log(error.message))


결과물은 무엇입니까? 


print message twice


** 생성자 메소드를 사용하여 Promise를 작성하고 reject 콜백으로 즉시 오류를 발생시킵니다.

* 그런 다음 .catch 핸들러는 여러 핸들러 콜백을 추가 할 수 있는 DOM의 .addEventListener (event, callback) 또는 Event Emitter의 .on (event, callback)과 같이 작동합니다. 각각 동일한 인수로 호출됩니다.


2. Multiple .catch’s 


var p = new Promise((resolve, reject) => {
  return Promise.reject(Error('The Fails!'))
})
p.catch(error => console.log(error.message))
p.catch(error => console.log(error.message))


결과물은 무엇입니까? 


처리되지 않은 약속 거부 경고(UnhandledPromiseRejectionWarning)


annotated-code/question-2.png 


Promise 생성자를 사용할 때는 resolve() 또는 reject() 콜백을 호출해야 합니다. Promise 생성자는 반환 값을 사용하지 않으므로 Promise.reject()로 생성 된 추가 Promise는 효과적으로 다시는 들을 수 없습니다.

Promise.reject() 뒤에 .catch가 없으면 대답은 UnhandledPromiseRejectionWarning입니다.


3. Chaining .then and .catch’s 


var p = new Promise((resolve, reject) => {
    reject(Error('The Fails!'))
  })
  .catch(error => console.log(error))
  .then(error => console.log(error))


결과물은 무엇입니까? 


print error and `undefined`


annotated-code/question-3.png 


.the와 .catch를 연결할 때 일련의 단계로 생각하면 도움이 됩니다. 각 .then은 이전 .then에서 반환 한 값을 인수로 받습니다. 그러나 "step"에 오류가 발생하면 .catch가 나타날 때까지 이후의 .then "steps"는 건너 뜁니다. 오류를 무시하려면 오류가 아닌 값을 반환하기 만하면 됩니다. 이후의 모든 .the를 통해 액세스 할 수 있습니다.


힌트 : console.log()는 항상 undefined를 반환합니다.


4. Chaining .catch’s 


var p = new Promise((resolve, reject) => {
    reject(Error('The Fails!'))
  })
  .catch(error => console.log(error.message))
  .catch(error => console.log(error.message))


결과물은 무엇입니까? 


print error message once


** .catch를 연결할 때 각각은 이전 .then 또는 .catch“단계”에서 발생한 오류 만 처리합니다. 

이 예에서 첫 번째 .catch는 console.log를 반환합니다.이 .catch는 .catch 's 뒤에 .then()을 추가해야만 액세스 할 수 있습니다.


5. Multiple .catch’s 


new Promise((resolve, reject) => {
    resolve('Success!')
  })
  .then(() => {
    throw Error('Oh noes!')
  })
  .catch(error => {
    return "actually, that worked"
  })
  .catch(error => console.log(error.message))


결과물은 무엇입니까? 


nothing prints


힌트 : .catch를 사용하면 단순히 일반 값을 반환하여 오류를 무시 (또는 무시) 할 수 있습니다.

이 트릭은 이후에 값을 받을 때만 작동합니다.


6. Flow between .then’s 


Promise.resolve('Success!')
  .then(data => {
    return data.toUpperCase()
  })
  .then(data => {
    console.log(data)
  })


결과물은 무엇입니까? 


print "SUCCESS!"


힌트 :. 그런 다음 반환 값에서 다음 .then (value => / * 핸들 값 * /)까지 순차적으로 데이터를 전달합니다.

다음에 값을 전달하기 위해서는 리턴 키가 필요합니다.


7. Flow between .then’s 


Promise.resolve('Success!')
  .then(data => {
    return data.toUpperCase()
  })
  .then(data => {
    console.log(data)
    return data
  })
  .then(console.log)


결과물은 무엇입니까? 


print "SUCCESS!" and "SUCCESS!"


8. Flow between .then’s 


Promise.resolve('Success!')
  .then(data => {
    data.toUpperCase()
  })
  .then(data => {
    console.log(data)
  })


결과물은 무엇입니까? 


prints `undefined`


힌트 :. 그런 다음 반환 값에서 다음 .then (value => / * 핸들 값 * /)까지 순차적으로 데이터를 전달합니다.


다음에 값을 전달하기 위해서는 리턴 키가 필요합니다.


9. Flow between .then’s and .catch’s 


Promise.resolve('Success!')
  .then(() => {
    throw Error('Oh noes!')
  })
  .catch(error => {
    return 'actually, that worked'
  })
  .then(data => {
    throw Error('The fails!')
  })
  .catch(error => console.log(error.message))


결과물은 무엇입니까? 


print "The fails!"


annotated-code/question-9-4.gif 



댓글목록 0

등록된 댓글이 없습니다.

웹학교 로고

온라인 코딩학교

코리아뉴스 2001 - , All right reserved.