promise和async都是做异步处理的, 使异步转为同步

1.promise

它和Promise诞生的目的都是为了解决“回调地狱”,

promise使用方法:

<button @click="testBtn()">点击</button>

  

    get(data) {
return new Promise((resolve, reject)=>{
if (data > 5) {
resolve(data);
} else {
reject("数据都是不大于5");
}
});
}, testF(num) {
console.log("=====", num)
}, // 调用
testBtn() {
this.get(6).then((num)=>{
this.testF(num);
});
this.get(3).then((num)=>{
this.testF(num);
});
}

  

2.async

async,会返回一个promise对象

  如果async函数中是return一个值,这个值就是Promise对象中resolve的值;

  如果async函数中是throw一个值,这个值就是Promise对象中reject的值。

async的使用方法:

async function imAsync(num) {
if (num > 0) {
return num // 这里相当于resolve(num)
} else {
throw num // 这里相当于reject(num)
}
} imAsync(1).then(function (v) {
console.log(v); // 1
}); // 注意这里是catch
imAsync(0).catch(function (v) {
console.log(v); // 0
})

  

3.await

await不会单独使用,他会和async一起使用, 如果直接使用await的话会不起作用,

await会暂停当前async函数的执行,等待后面的Promise的计算结果返回以后再继续执行当前的async函数

使用场景:

在发起请求获取数据的时候,如果个return返回数据, 这时就需要用到await

async test(){
const currentArr = []
await this.$axios
.get("/topsellerCategorys/", {
marketplaceID: item.value,
listname: "AnyDepartment"
})
.then(response => {
if (response && response.data) {
currentArr = response.data
);
}
})
.catch(error => {
this.loadingShow = false;
console.log(error);
});
if (this.fristCategory[0] && this.fristCategory[0].length) {
this.selectArr = [this.fristCategory[0][0].label]; //类目默认赋值
this.category = this.fristCategory[0][0].label; //选择的类目命默认赋值
}
return currentArr
}

  

  

promise和async/await的用法的更多相关文章

  1. 异步操作之 Promise 和 Async await 用法进阶

    ES6 提供的 Promise 方法和 ES7 提供的 Async/Await 语法糖都可以更好解决多层回调问题, 详细用法可参考:https://www.cnblogs.com/cckui/p/99 ...

  2. promise 进阶 —— async / await 结合 bluebird

    一.背景 1.Node.js 异步控制 在之前写的 callback vs async.js vs promise vs async / await 里,我介绍了 ES6 的 promise 和 ES ...

  3. Promise 和async/await 的使用理解

    Promise 和async/await 的使用理解 1. new Promise时就会开始执行语句. new Promise(resolve => resolove('成功信息') )     ...

  4. 20分钟带你掌握JavaScript Promise和 Async/Await

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://www.freecodecamp.org/news/learn-promise-a ...

  5. Promise和async await详解

    本文转载自Promise和async await详解 Promise 状态 pending: 初始状态, 非 fulfilled 或 rejected. fulfilled: 成功的操作. rejec ...

  6. node.js异步控制流程 回调,事件,promise和async/await

    写这个问题是因为最近看到一些初学者用回调用的不亦乐乎,最后代码左调来又调去很不直观. 首先上结论:推荐使用async/await或者co/yield,其次是promise,再次是事件,回调不要使用. ...

  7. Promise, Generator, async/await的渐进理解

    作为前端开发者的伙伴们,肯定对Promise,Generator,async/await非常熟悉不过了.Promise绝对是烂记于心,而async/await却让使大伙们感觉到爽(原来异步可以这么简单 ...

  8. Promise及Async/Await

      一.为什么有Async/Await? 我们都知道已经有了Promise的解决方案了,为什么还要ES7提出新的Async/Await标准呢? 答案其实也显而易见:Promise虽然跳出了异步嵌套的怪 ...

  9. callback vs async.js vs promise vs async / await

    需求: A.依次读取 A|B|C 三个文件,如果有失败,则立即终止. B.同时读取 A|B|C 三个文件,如果有失败,则立即终止. 一.callback 需求A: let read = functio ...

随机推荐

  1. CodeForces-916A-jamie and Alarm Snooze(笨比题目)

    链接: https://vjudge.net/problem/CodeForces-916A 题意: Jamie loves sleeping. One day, he decides that he ...

  2. Comet OJ - Contest #8 E.神奇函数

    旧题解:https://blog.csdn.net/gmh77/article/details/99066792#commentBox 之前写的有些奇怪,不能体现这道题的sb所以再推一遍 \(\bec ...

  3. select 语句关键字优先级

    1.select 语句优先级 select classid, userid, name, age from study where userid > 15 group by classid ha ...

  4. 树状数组(Binary Indexed Tree)

    树状数组(Binary Indexed Tree,BIT) 是能够完成下述操作的数据结构. 给一个初始值全为 0 的数列 a1, a2, ..., an (1)给定 i,计算 a1+a2+...+ai ...

  5. 【gym102394B】Binary Numbers(DP)

    题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...

  6. mac 的 ping 命令怎么停掉?

    Widnows下的Ping不会只Ping4次,mac 会不停的Ping下去,需要停止,按下键盘上的 control+c 键即可停掉 Ping 过程.

  7. Apache配置详解(最好的APACHE配置教程)

    From: http://aiks.blog.com.cn/archives/2006/1748482.shtml Apache的配置 Apache的配置由httpd.conf文件配置,因此下面的配置 ...

  8. http中post 和 get 请求方法区别

    前言 做Web开发就一定会涉及到浏览器和服务器的交互,所以了解浏览器和服务器交互的方式就尤为重要.从接触B/S开始就已经接触到了get和post,但是对它们的了解确实不深入.在后来不断的做项目过程中, ...

  9. 关于spring data jpa的@query的传入参数是对象怎么匹配参数

    /** * Specifies methods used to obtain and modify person related information * which is stored in th ...

  10. js fuction函数内return一个内部函数详解

    今天在网上,看到一篇关于js函数难点的文章,js函数的一些难点.在那上面提了一下,关于js函数返回另一个函数的问题,并附上了一道面试题: var add = function(x){ var sum  ...