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

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()
    }
  })



)

各位看官,自个理会.