最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序使用Promise簡化回調(diào)

 更新時間:2018年02月06日 09:30:16   作者:tomfriwel  
本篇文章主要介紹了微信小程序使用Promise簡化回調(diào),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Promise 是異步編程的一種解決方案,比傳統(tǒng)的解決方案——回調(diào)函數(shù)和事件——更合理和更強大。它由社區(qū)最早提出和實現(xiàn),ES6 將其寫進了語言標(biāo)準(zhǔn),統(tǒng)一了用法,原生提供了Promise對象。

所謂Promise,簡單說就是一個容器,里面保存著某個未來才會結(jié)束的事件(通常是一個異步操作)的結(jié)果。從語法上說,Promise 是一個對象,從它可以獲取異步操作的消息。Promise 提供統(tǒng)一的 API,各種異步操作都可以用同樣的方法進行處理。

了解什么是 Promise 對象

在項目中,會出現(xiàn)各種異步操作,如果一個異步操作的回調(diào)里還有異步操作,就會出現(xiàn)回調(diào)金字塔。

比如下面這種

// 模擬獲取code,然后將code傳給后臺,成功后獲取userinfo,再將userinfo傳給后臺
// 登錄
wx.login({
  success: res => {
    let code = res.code
    // 請求
    imitationPost({
      url: '/test/loginWithCode',
      data: {
        code
      },
      success: data => {
        // 獲取userInfo
        wx.getUserInfo({
          success: res => {
            let userInfo = res.userInfo
            // 請求
            imitationPost({
              url: '/test/saveUserInfo',
              data: {
                userInfo
              },
              success: data => {
                console.log(data)
              },
              fail: res => {
                console.log(res)
              }
            })
          },
          fail: res => {
            console.log(res)
          }
        })
      },
      fail: res => {
        console.log(res)
      }
    })
  },
  fail: res => {
    console.log(res)
  }
})

下面分析如何用Promise來進行簡化代碼

因為微信小程序異步api都是success和fail的形式,所有有人封裝了這樣一個方法:

promisify.js

module.exports = (api) => {
  return (options, ...params) => {
    return new Promise((resolve, reject) => {
      api(Object.assign({}, options, { success: resolve, fail: reject }), ...params);
    });
  }
}

先看最簡單的:

// 獲取系統(tǒng)信息
wx.getSystemInfo({
  success: res => {
    // success
    console.log(res)
  },
  fail: res => {

  }
})

使用上面的promisify.js簡化后:

const promisify = require('./promisify')
const getSystemInfo = promisify(wx.getSystemInfo)

getSystemInfo().then(res=>{
  // success
  console.log(res)
}).catch(res=>{

})

getSystemInfo

可以看到簡化后的回調(diào)里少了一個縮進,并且回調(diào)函數(shù)從9行減少到了6行。

回調(diào)金字塔的簡化效果

那么再來看看最開始的那個回調(diào)金字塔

const promisify = require('./promisify')
const login = promisify(wx.login)
const getSystemInfo = promisify(wx.getSystemInfo)

// 登錄
login().then(res => {
  let code = res.code
  // 請求
  pImitationPost({
    url: '/test/loginWithCode',
    data: {
      code
    },
  }).then(data => {
    // 獲取userInfo
    getUserInfo().then(res => {
      let userInfo = res.userInfo
      // 請求
      pImitationPost({
        url: '/test/saveUserInfo',
        data: {
          userInfo
        },
      }).then(data => {
        console.log(data)
      }).catch(res => {
        console.log(res)
      })
    }).catch(res => {
      console.log(res)
    })
  }).catch(res => {
    console.log(res)
  })
}).catch(res => {
  console.log(res)
})

簡化回調(diào)

可以看到簡化效果非常明顯。

同樣適用于網(wǎng)頁或者nodejs等中。

參考

Promise 對象

源代碼

tomfriwel/MyWechatAppDemo 的promisePage頁面

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

南部县| 望谟县| 陈巴尔虎旗| 沁阳市| 侯马市| 清水县| 梅州市| 井冈山市| 勐海县| 东阿县| 正蓝旗| 武威市| 平昌县| 沽源县| 漯河市| 东港市| 武功县| 依兰县| 广南县| 平湖市| 抚远县| 兴仁县| 永吉县| 东兰县| 长乐市| 芷江| 贞丰县| 伊金霍洛旗| 松桃| 芒康县| 武强县| 镇沅| 江永县| 密云县| 西乌珠穆沁旗| 新河县| 延安市| 铜陵市| 扎鲁特旗| 治县。| 清水县|