NullFn = () => {}; console.log(wx); function IllegalAPIException(name) { this.message = "No Such API [" + name + "]"; this.name = 'IllegalAPIException'; } let services = { sleep: (time) => { return new Promise(function (resolve, reject) { setTimeout(resolve, time); }) }, stop: () => { return new Promise(function (resolve, reject) { }) }, taskSequence: () => { return new Promise(function (resolve, reject) { resolve() }) } }; export let wsAPI = new Proxy(services, { get: function (target, property) { console.log(property,target); if (property in target) { return target[property]; } else if (property in wx) { return (obj) => { return new Promise(function (resolve, reject) { obj = obj || {}; obj.success = (... args) => { resolve(... args) }; obj.fail = (... args) => { reject(... args); }; obj.complete = nullFn; wx[property](obj); }); } } else { throw new IllegalAPIException(property); }}}); import {wsAPI} from ".. /.. // utils/wxApi" // module. Exports // const wsAPI = require(".. /.. /utils/wsAPI.js")Copy the code
Example:
wsAPI.taskSequence() .then(() => wsAPI.showLoading({title: "Save"})). Then (() = > wsAPI. Sleep (1000)), then (() = > wsAPI. HideLoading ()), then (() = > wsAPI. Sleep (500)). Then (() = > wsAPI.showLoading({title: "Load"})). Then (() = > wsAPI. Sleep (1000)), then (() = > wsAPI. HideLoading ()), then (() = > console. The log (" done "))Copy the code

Function (A){B} =>{B}; WsAPI rewraps all of wX’s apis with Proxy (ECMAScript 6 Starter/Proxy). Sleep, Stop, and taskSequence have been added. Sleep is used to block a period of time; The taskSequence is an empty Promise that makes the code look neat and readable (Example 4). Stop is used to stop the task sequence

Reprinted from:My.oschina.net/u/3396785/b…