vue中使用promise、gennerator、async/await
2019-03-08 17:01
323 查看
注:json格式请参考图片上格式。一、代码
Promise封装axios
promiseAxios(url, method = 'get', data = {}) { return new Promise((resolve, reject) => { axios({ url, method, data }).then(res => { resolve(res.data) }).catch(err => { reject(err) }) }) },
1.Promise http://es6.ruanyifeng.com/#docs/promise
peromiseFn() { this.promiseAxios('json/demo3.json', 'get', this.param).then(res => { console.log(res) return this.promiseAxios('json/demo2.json', 'get', this.param) }).then(res => { console.log(res) }) }
2.Generator http://es6.ruanyifeng.com/#docs/generator
// 定义 * generator() { yield this.promiseAxios('json/demo1.json', 'get', this.param) yield this.promiseAxios('json/demo2.json', 'get', this.param) yield this.promiseAxios('json/demo3.json', 'get', this.param) } // 获取 generatorFn() { let getData = this.generator() getData.next().value.then(res => { console.log(res.employees) return getData.next().value }).then(res => { console.log(res.list) return getData.next().value }) .then(res => { console.log(res.list) }) }
3.Async/await http://es6.ruanyifeng.com/#docs/async
async asyncFn() { // 使用解构,不了解的同学,请看 http://es6.ruanyifeng.com/#docs/destructuring let [a, b, c] = await Promise.all([ this.promiseAxios('json/demo1.json', 'get', this.param), this.promiseAxios('json/demo2.json', 'get', this.param), this.promiseAxios('json/demo3.json', 'get', this.param)]) this.getPromiseData = a this.getGeneratorData = b this.getAsyncData = c }二、全部html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <style type="text/css"> p>span { color: red; font-weight: bold; } </style> </head> <body> <section id="view" clock> <p><span>promise</span>: {{getPromiseData}}</p> <p><span>generator</span>: {{getGeneratorData}}</p> <p><span>async</span>: {{getAsyncData}}</p> </section> <script type="text/javascript"> const vm = new Vue({ el: '#view', data() { return { param: {}, getPromiseData: [], getGeneratorData: [], getAsyncData: [] } }, mounted() { this.peromiseFn() this.asyncFn() this.generatorFn() }, methods: { // 封装 axios promiseAxios(url, method = 'get', data = {}) { return new Promise((resolve, reject) => { axios({ url, method, data }).then(res => { resolve(res.data) }).catch(err => { reject(err) }) }) }, // 1. promise peromiseFn() { this.promiseAxios('json/demo3.json', 'get', this.param).then(res => { console.log(res) return this.promiseAxios('json/demo2.json', 'get', this.param) }).then(res => { console.log(res) }) }, // 2. async ==> await async asyncFn() { let [a, b, c] = await Promise.all([this.promiseAxios('json/demo1.json', 'get', this.param), this.promiseAxios( 'json/demo2.json', 'get', this.param), this.promiseAxios( 'json/demo3.json', 'get', this.param)]) console.log(a, b, c) this.getPromiseData = a this.getGeneratorData = b this.getAsyncData = c }, // 3. generator * generator() { yield this.promiseAxios('json/demo1.json', 'get', this.param) yield this.promiseAxios('json/demo2.json', 'get', this.param) yield this.promiseAxios('json/demo3.json', 'get', this.param) }, generatorFn() { let getData = this.generator() getData.next().value.then(res => { console.log(res.employees) return getData.next().value }).then(res => { console.log(res.list) return getData.next().value }) .then(res => { console.log(res.list) }) } } }) </script> </body> </html>
相关文章推荐
- Promise 、Async/Await的使用
- 详解koa2学习中使用 async 、await、promise解决异步的问题
- node-使用promise, generator, async/await 读取文件的方法
- Promise、Async/Await的使用
- vue 钩子函数 使用async await
- async/await - 6.使用Promise.all()让多个await操作并行
- ES6 Promise 和 Async/await的使用
- 使用Typescript写的Vue初学者Hello World实例(实现按需加载、跨域调试、await/async)
- 详解vue中async-await的使用误区
- 使用Promise和async-await实现的一个异步遍历+同步执行任务的实例
- 异步编程 -- 使用Promise、Generator、async/await 规避回调地狱
- vue:异步async and await与this.nextTick()的使用
- asp.net webform中使用async,await实现异步操作
- Callback Promise Generator Async-Await 和异常处理的演进_1
- await和async在C#一般处理程序(ashx)中的使用
- 前端的异步解决方案之Promise和Await-Async
- [.NET 4.5] ADO.NET / ASP.NET 使用 Async 和 Await 异步 存取数据库
- Async/Await替代Promise的6个理由
- 如何在微信小程序中使用async/await
- 用async-await实现类似Promise.all()的执行效果