
本文共 2412 字,大约阅读时间需要 8 分钟。
What is Promise?
Promise������������������������������������������������������������������������������������������������������������������Promise������������������������������������������������������������������������������������������������������������������then���catch���������������������������������������������������������������������������������������������������������������������������
Promise���������������
Promise������������������������
1. pending���������������������������������������������������������������������
2. fulfilled������������������������������������������������������
3. rejected���������������������������������������������������������������������������������������������������������
Promise������������������������������������������������������������������������pending������������������fulfilled���rejected������������������������������������������������������������������������������������������
Using Promise to Wrap wx.request()
������������������wx.request()������������Promise������������������������������
1. ������http���������������������������������������������������������http���������������
2. ������������������http���������������������api.js���fetch.js���http.js���������
3. ���api.js���������������������������������������������������������������������������������������������
module.exports = { home: '/home/multidata', a: '/a/aaaa', b: '/b/bbb' }
4. ���fetch.js���������Promise������������wx.request()�������������������������������Promise������������������������
module.exports = (url, data, method) => { return new Promise((resolve, reject) => { wx.request({ url, data, method: method, success(res) { resolve(res); }, fail(err) { reject(err); } }); }); }
5. ���http.js���������������������������������������������������baseUrl������������fetch������Promise������������������������������������������������
const p = require('./fetch'); const apiList = require('./api'); const baseUrl = 'http://123.207.32.32:8000/api/h8'; module.exports = { banner() { return p(baseUrl + apiList.home, {}, 'get'); } };
6. ���app.js���������������������������������������������http���������
const http = require('./http/http'); APP({ http });
7. ������������������������������������������������������onLoad������������������
data: {}, onLoad: function(options) { app.http.banner().then(res => { const { data: { data: { banner: { list } } } = res; this.setData({ arr: list }); }); }
发表评论
最新留言
关于作者
