• Promise intro ang creating promises


    refer to: https://www.udemy.com/course/the-web-developer-bootcamp

    • Promise
      • A promise is an object representing the eventual completion or failure of an asynchronous operation.
      • A PATTERN for writing async code
      • Resolve and reject
      • more details : https://www.runoob.com/w3cnote/javascript-promise-object.html
      • / THE CALLBACK VERSION
        const fakeRequestCallback = (url, success, failure) => {
            const delay = Math.floor(Math.random() * 4500) + 500;
            setTimeout(() => {
                if (delay > 4000) {
                    failure('Connection Timeout :(')
                } else {
                    success(`Here is your fake data from ${url}`)
                }
            }, delay)
        }
        // THE PROMISE VERSION 
        const fakeRequestPromise = (url) => {
            return new Promise((resolve, reject) => {
                const delay = Math.floor(Math.random() * (4500)) + 500;
                setTimeout(() => {
                    if (delay > 4000) {
                        reject('Connection Timeout :(')
                    } else {
                        resolve(`Here is your fake data from ${url}`)
                    }
                }, delay)
            })
        }
    • .then.  .catch

    • // fakeRequestPromise('yelp.com/api/coffee/page1') // .then(() => { // console.log("IT WORKED!!!!!! (page1)") // fakeRequestPromise('yelp.com/api/coffee/page2') // .then(() => { // console.log("IT WORKED!!!!!! (page2)") // fakeRequestPromise('yelp.com/api/coffee/page3') // .then(() => { // console.log("IT WORKED!!!!!! (page3)") // }) // .catch(() => { // console.log("OH NO, ERROR!!! (page3)") // }) // }) // .catch(() => { // console.log("OH NO, ERROR!!! (page2)") // }) // }) // .catch(() => { // console.log("OH NO, ERROR!!! (page1)") // }) // THE CLEANEST OPTION WITH THEN/CATCH // RETURN A PROMISE FROM .THEN() CALLBACK SO WE CAN CHAIN! fakeRequestPromise('yelp.com/api/coffee/page1') .then((data) => { console.log("IT WORKED!!!!!! (page1)") console.log(data) return fakeRequestPromise('yelp.com/api/coffee/page2') }) .then((data) => { console.log("IT WORKED!!!!!! (page2)") console.log(data) return fakeRequestPromise('yelp.com/api/coffee/page3') }) .then((data) => { console.log("IT WORKED!!!!!! (page3)") console.log(data) }) .catch((err) => { console.log("OH NO, A REQUEST FAILED!!!") console.log(err) })
    • creating promises
    • const fakeRequest = (url) => {
          return new Promise((resolve, reject) => {
              const rand = Math.random();
              setTimeout(() => {
                  if (rand < 0.7) {
                      resolve('YOUR FAKE DATA HERE');
                  }
                  reject('Request Error!');
              }, 1000)
          })
      }
      
      fakeRequest('/dogs/1')
          .then((data) => {
              console.log("DONE WITH REQUEST!")
              console.log('data is:', data)
          })
          .catch((err) => {
              console.log("OH NO!", err)
          })
    • using promise to creat rainbow change color

    • const delayedColorChange = (color, delay) => {
          return new Promise((resolve, reject) => {
              setTimeout(() => {
                  document.body.style.backgroundColor = color;
                  resolve();
              }, delay)
          })
      }
      
      
      delayedColorChange('red', 1000)
          .then(() => delayedColorChange('orange', 1000))
          .then(() => delayedColorChange('yellow', 1000))
          .then(() => delayedColorChange('green', 1000))
          .then(() => delayedColorChange('blue', 1000))
          .then(() => delayedColorChange('indigo', 1000))
          .then(() => delayedColorChange('violet', 1000))
  • 相关阅读:
    更新 箫心病毒分析专家2006 build 5.23(C#2.0)
    SOA也是一种设计模式
    SOA在美国和中国的差异
    单服务器配置部署Visual Studio 2005 Team Foundation Server步骤
    无废话ExtJs 入门教程十六[页面布局:Layout]
    无废话ExtJs 入门教程七[登陆窗体Demo:Login]
    无废话ExtJs 入门教程十八[树:TreePanel]
    无废话ExtJs 入门教程十一[下拉列表:Combobox]
    无废话ExtJs 入门教程十[单选组:RadioGroup、复选组:CheckBoxGroup]
    无废话ExtJs 入门教程八[脚本调试Firefox:firebug]
  • 原文地址:https://www.cnblogs.com/LilyLiya/p/14329815.html
Copyright © 2020-2023  润新知