码迷,mamicode.com
首页 > 微信 > 详细

微信小程序 - cb回调(typeof cb == "function" && cb(obj);)

时间:2018-12-19 15:45:42      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:微信小程序   ror   lis   cat   head   encode   for   function   封装   

typeof cb == "function" && cb(obj)

 

但凡用了Promise,这种方式就可以抛弃了.

Page({
  data: {},
  onLoad() {

    request(‘https://api.it120.cc/jy02149522/banner/list‘, {
      type: 0
    }, function(obj) {
      console.log(‘请求到的数据:‘, obj)
    })
  }
})



// request请求封装
const request = (url, data, cb) => (
  wx.request({
    url,
    data,
    method: ‘GET‘,
    header: {
      ‘content-type‘: ‘application/x-www-form-urlencoded‘
    },
    dataType: ‘json‘,
    responseType: ‘text‘,
    success(res) {
      //判断cb是不是function,并且执行这行这个function
      res.data.code == 0 ? typeof cb == "function" && cb(res.data) : typeof error === ‘function‘ && error()
    },
    fail() {
      typeof error === ‘function‘ && error()
    },
    complete(res) {
      // res.data.code != 0 ? typeof cb == "function" && cb(res.data) : typeof error === ‘function‘ && error()
    }
  })



)

 

各位看官,自个理会.

微信小程序 - cb回调(typeof cb == "function" && cb(obj);)

标签:微信小程序   ror   lis   cat   head   encode   for   function   封装   

原文地址:https://www.cnblogs.com/cisum/p/10142917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!