小程序使用 async await

栏目: JavaScript · 发布时间: 7年前

  • 第一个问题: 虽然小程序不支持,但是我们可以引入js库啊。双手奉上facebook的开源库regenerator 下载'packages/regenerator-runtime'这个路径下的runtime.js,放到自己小程序项目下的utils或者lib文件夹下。

  • 第二个问题: Async跟Await的用法

  • Async - 定义异步函数(async function someName(){...})

    • 自动把函数转换为 Promise
    • 当调用异步函数时,函数返回值会被 resolve 处理
    • 异步函数内部可以使用 await
  • Await - 暂停异步函数的执行 (var result = await someAsyncCall();)

    • 当使用在 Promise 前面时,await 等待 Promise 完成,并返回 Promise 的结果
    • await 只能和 Promise 一起使用,不能和 callback 一起使用
    • await 只能用在 async 函数中
import regeneratorRuntime from '../../utils/runtime.js'

onLoad: function() {
    this.initData();
  },

  async initData(){
	await this.initMyData();//请求接口1
	await this.initTodayData();//请求接口2
  }

  initMyData:function(){
	console.log('开始请求1')
	........
	//回调函数的方法内写
	console.log("完成请求1")
	
	
}

  initTodayData:function(){
	console.log('开始请求2')
	........
	//回调函数的方法内写
	console.log("完成请求2")
}
复制代码

以上所述就是小编给大家介绍的《小程序使用 async await》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

About Face 3

About Face 3

Alan Cooper、Robert Reimann、David Cronin / John Wiley & Sons / 2007-5-15 / GBP 28.99

* The return of the authoritative bestseller includes all new content relevant to the popularization of how About Face maintains its relevance to new Web technologies such as AJAX and mobile platforms......一起来看看 《About Face 3》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具