Guest User

Untitled

a guest
Dec 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. By now, you've seen that much of JavaScript is based in events. Because our code is running asynchronously, we can't always be sure how long things will take to finish. So far, we've dealt with this using callback functions. A callback is just a function that we call once another procedure is completed. For example, setTimeout is a JavaScript function that takes two arguments, a function and an amount of time in milliseconds. Once that amount of time has gone by, they call the function for us.
  2.  
  3. setTimeout(function(){
  4. console.log("This will be called after two seconds!")
  5. }, 2000)
  6. ******************************************************************
  7. Promises are native javascript objects that wrap around slow procedures. Our promises will respond to a method called then which will fire a function whenever a procedure has finished.
  8.  
  9. To start out, we can instantiate a new Promise and pass in a function. This function will take two callback functions as arguments - one for if our procedure works and one for if it doesn't work. We'll refer to these as resolve and reject
  10.  
  11. ***********************************************************
  12. Promises lab
  13.  
  14. const questions = [
  15. {questionText: "Lightning never strikes in the same place twice", answer: false},
  16. {questionText: "Humans can distinguish between over one trillion different smells", answer: true},
  17. {questionText: "Goldfish only have a memory of about three seconds", answer: false}
  18. ]
  19.  
  20. let question;
  21.  
  22. function appendQuestion(question) {
  23. let container = document.querySelector('.question-container')
  24. container.innerHTML = question.questionText
  25. }
  26.  
  27. function askQuestionThen(time) {
  28. question = questions[0]
  29. appendQuestion(question)
  30. return new Promise(function (resolve) {
  31. setTimeout(function () {
  32. resolve(question)
  33. }, time)
  34. })
  35. }
  36.  
  37. function removeQuestion() {
  38. return new Promise(function () {
  39. let container = document.querySelector('.question-container')
  40. container.innerHTML = ''
  41. toggleTrueAndFalseButtons()
  42. })
  43. }
  44.  
  45. function askQuestionThenRemoveQuestion(time) {
  46. return askQuestionThen(time).then(removeQuestion)
  47. }
  48.  
  49. function trueAndFalseButtons() {
  50. return btns = document.querySelector('.true-false-list').querySelectorAll('.btn')
  51. }
  52. function toggleTrueAndFalseButtons() {
  53. trueAndFalseButtons().forEach(function (btn) {
  54. btn.classList.toggle("hide")
  55. })
  56. }
  57. function checkQuestion(question, answer) {
  58. question.questionAnswer == answer
  59. }
  60.  
  61. function displayQuestionOnClick() {
  62. let btn = document.querySelector("a");
  63. return btn.addEventListener("click", () => {
  64. toggleTrueAndFalseButtons();
  65. askQuestionThenRemoveQuestion(5000);
  66. });
  67. }
Add Comment
Please, Sign In to add comment