How to encapsulate the front-end promise in WeChat Mini Programs
This article mainly explains "how to encapsulate the front-end promise in WeChat Mini Programs". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to encapsulate the front-end promise in WeChat Mini Programs.
Config.js
Const config = {base_url_api: "https://douban.uieee.com/v2/movie/",}export {config}
Http.js
Import {config} from ".. / config" Class HTTP {requset ({url, method = "GET", data = {}}) {const promise = new Promise ((resolve, reject) = > {wx.request ({url: config.base_url_api + url, data, method, header: {'Content-Type':' json'}) Success: res = > {/ / status code toString () is converted to the string const statusCode = res.statusCode.toString () If (statusCode.startsWith ("2")) {resolve (res.data)} else {this._show_error ();}}, fail: res = > {reject (err); this._show_error ();}}) return promise } _ show_error () {wx.showToast ({title: 'network error', icon: 'none'})}} export {HTTP}
Model/movie.js
Import {HTTP} from ".. / utils/http"; class MovieModel extends HTTP {getInTheaters () {return this.requset ({url: "in_theaters"})} getTop250 () {return this.requset ({url: "top250"})} getComingSoon () {return this.requset ({url: "coming_soon"})} export {MovieModel}
Pages/index/index.js
Const app = getApp (); import {MovieModel} from ".. /.. / model/movie"; const movieModel = new MovieModel (); Page ({onLoad () {/ / movieModel.getInTheaters (). Then (res= > {/ / console.log (res) / /}) const inTheaters = movieModel.getInTheaters () const top250 = movieModel.getTop250 (); const comingSoon = movieModel.getComingSoon (); Promise.all ([inTheaters,top250,comingSoon]) .then (res= > {let [inTheaters,top250,comingSoon] = res Console.log (inTheaters)})}}) Thank you for your reading. The above is the content of "how to encapsulate the front-end promise in WeChat Mini Programs". After the study of this article, I believe you have a deeper understanding of how to encapsulate the front-end promise in WeChat Mini Programs, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!