async await promise

119 阅读1分钟
  • 同步:执行顺序和任务排列顺序一致
  • 异步:跳过,继续执行(依次唤醒,先返回先执行)
```
data() {
			return {
				arr: ['张三', '李四', '王二', '麻子'],
				getName: ''
			}
		},
		onShow() {
			this.setName();
		},
		methods: {
			getIndex() {
				return new Promise((resolve, reject) => {
					setTimeout(function() {
						let _index = parseInt(Math.random() * 4);
						console.log("_index",_index)
						resolve(_index)
					}, 1000);
				})
			},
			async setName() {
				let n =await this.getIndex();
				console.log('n', n)
				setTimeout(() => {
					this.getName = this.arr[n];
				}, 0)
			}
		}
```