测试Promise与Async/await的基本使用
想在项目中用, 发现自己不是很熟
promise基本使用
基本使用-思路
new Promise()返回了一个状态机- 一个完全无法被外界影响的状态机
- 构造函数, 传入一个函数, 两个参数, 分别是
reslove, reject - 表示执行的回调函数, 也就是
.then(),.cache()函数 - 达到内部调用外部函数的作用, 行程异步
function init () {
var p = new Promise((resolve, reject) => {
reject(new Error(1))
})
p.then(a => {
console.log(a)
}).catch(b => {
console.log(b)
})
console.log(p)
}
init()
基本使用-注意事项
- 命名不可错误
- reject回调函数中, 尽量放入一个
new Error - 如果直接在
new Promise()的构造函数中,throw new Error() - 也不会触发程序的停止, 而是被外面的
.cache()所捕获 - 如果函数没有进行
.cache()的话, 会抛出异常 - 但不会影响其他程序的执行
function init () {
var p = new Promise((resolve, reject) => {
throw new Error(1)
})
p.then(a => {
console.log(a)
}).catch(e => {
console.log(e) // Error: 1
})
}
init()
function init () {
var p = new Promise((resolve, reject) => {
throw new Error(1)
})
var p1 = new Promise((resolve, reject) => {
resolve(1)
})
p.then(a => {
console.log(a)
})
p1.then(a => {
console.log(a)
})
setTimeout(() => {
console.log(3)
}, 1000)
}
init()
console.log(2)
// 2
// 1
// Error: 1(抛出异常)
// (1s之后)
// 3
async函数
async函数-直接返回new Promise
- async 函数直接返回了新new的Promise
async function a () {
return new Promise((resolve, reject) => {
resolve(2)
})
}
function init () {
setTimeout(() => {
console.log(0)
}, 0)
var p = a()
p.then(a => {
console.log(a)
})
console.log(1)
}
init()
// 1
// 2
// 0
- 测试
new Promise中抛出错误, 是否影响执行
async function a () {
return new Promise((resolve, reject) => {
throw new Error(2)
})
}
function init () {
setTimeout(() => {
console.log(0)
}, 0)
var p = a()
p.then(a => {
console.log(a)
})
console.log(1)
}
init()
// 1
// 报错: Uncaught (in promise) Error: 2
// 0
async/await测试
await的基本使用
- await返回的不再是Promise
async function a () {
return new Promise((resolve, reject) => {
resolve(1)
})
}
async function init () {
setTimeout(() => {
console.log(0)
}, 0)
var p = await a()
p.then(a => {
console.log(a)
})
console.log(1)
}
init()
// Uncaught (in promise) TypeError: p.then is not a function
// 0
- 使用reslove返回正确的结果
async function a () {
return new Promise((resolve, reject) => {
resolve(2)
})
}
async function init () {
setTimeout(() => {
console.log(0)
}, 0)
console.log(1)
var p = await a()
console.log(p)
}
init()
// 1
// 2
// 0
- 如果在
new Promise的时候, 发生错误, 需要捕获await
async function a () {
return new Promise((resolve, reject) => {
throw new Error('err') // reject(new Error('err')) 同样的效果
})
}
async function init () {
try {
var p = await a()
} catch (err) {
console.log(err)
}
console.log(p)
}
init()
// Error: err
// undefined
- 可以在trycache的时候, 捕获了再抛出
- 若外边的函数, 未进行捕获处理, 将影响程序执行
async function a () {
return new Promise((resolve, reject) => {
try {
throw new Error('err')
} catch (error) {
reject(error)
}
})
}
async function init () {
var p = await a()
console.log(p)
}
init()
// 报错: Uncaught (in promise) Error: err
// 并且程序不会再继续执行
- 未使用async函数, 直接返回不是Promise的值, 接await可以直接输出
async function a () {
return 2
}
async function init () {
var p = await a()
console.log(p)
}
init()
- 只要使用了await就需要等待执行完, 返回了结果
async function a () {
var p = await b()
console.log(p)
return 'p'
}
async function b () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1)
}, 2000)
})
}
async function init () {
var p = await a()
console.log(p)
}
init()
// (2s)
// 1
// 'p'
测试Promise与Async/await的基本使用的更多相关文章
- Promise及Async/Await
一.为什么有Async/Await? 我们都知道已经有了Promise的解决方案了,为什么还要ES7提出新的Async/Await标准呢? 答案其实也显而易见:Promise虽然跳出了异步嵌套的怪 ...
- 异步Promise及Async/Await最完整入门攻略
一.为什么有Async/Await? 我们都知道已经有了Promise的解决方案了,为什么还要ES7提出新的Async/Await标准呢? 答案其实也显而易见:Promise虽然跳出了异步嵌套的怪圈, ...
- 异步Promise及Async/Await可能最完整入门攻略
此文只介绍Async/Await与Promise基础知识与实际用到注意的问题,将通过很多代码实例进行说明,两个实例代码是setDelay和setDelaySecond. tips:本文系原创转自我的博 ...
- Promise和async await详解
本文转载自Promise和async await详解 Promise 状态 pending: 初始状态, 非 fulfilled 或 rejected. fulfilled: 成功的操作. rejec ...
- node.js异步控制流程 回调,事件,promise和async/await
写这个问题是因为最近看到一些初学者用回调用的不亦乐乎,最后代码左调来又调去很不直观. 首先上结论:推荐使用async/await或者co/yield,其次是promise,再次是事件,回调不要使用. ...
- Promise, Generator, async/await的渐进理解
作为前端开发者的伙伴们,肯定对Promise,Generator,async/await非常熟悉不过了.Promise绝对是烂记于心,而async/await却让使大伙们感觉到爽(原来异步可以这么简单 ...
- 异步操作之 Promise 和 Async await 用法进阶
ES6 提供的 Promise 方法和 ES7 提供的 Async/Await 语法糖都可以更好解决多层回调问题, 详细用法可参考:https://www.cnblogs.com/cckui/p/99 ...
- callback vs async.js vs promise vs async / await
需求: A.依次读取 A|B|C 三个文件,如果有失败,则立即终止. B.同时读取 A|B|C 三个文件,如果有失败,则立即终止. 一.callback 需求A: let read = functio ...
- Callback, Promise和Async/Await的对比
Callback, Promise和Async/Await的对比 Callback Hell getData1(function (data1) { console.log('我得到data1了') ...
随机推荐
- MySQL 聚合函数(四)检测功能依赖
源自MySQL 5.7 官方手册:12.20.4 Detection of Functional Dependence 本节提供了MySQL检测功能依赖的方式的几个示例.这些示例使用此表示法: {X} ...
- JDK + Tomcat 安装 + 制作自定义镜像【第 3 篇 系统镜像】
[第 1 篇 JDK]:https://www.cnblogs.com/del88/p/11842387.html[第 2 篇 Tomcat]:https://www.cnblogs.com/del8 ...
- queryURLParams
let url = 'http://www.douqu.com/index.html?name1=val1&name2=val2'; //1.提取问号后的字符 let asktext = ur ...
- world 页面横向
今天浏览world时,发现一个现象: 出现的页面比较宽,如何做的呢? 操作如下: 1.打开一个新的world 2.选择布局页签,分隔符,分节符的下一页 3.鼠标放到第二页上,选择页签布局,纸张方向,选 ...
- PAT Basic 1057 数零壹 (20 分)
给定一串长度不超过 1 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例如给定 ...
- Django 语法笔记
Django 语法 创建项目框架 django-admin startproject 项目名 创建子app 业务分化,可以优化团队合作,可以明确找锅 python manage.py startapp ...
- vue-cli3项目中使用CDN
1.html 中引入 echarts html中添加script标签如下: <script src="//cdn.bootcss.com/echarts ...
- Comparator分组测试
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.u ...
- Windows环境下使用uiautomatorviewer进行元素定位
一.摘要 元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作,uiautomatorviewer 是 android-sdk 自带的一个元 ...
- C#实现异步阻塞TCP(SocketAsyncEventArgs,SendAsync,ReceiveAsync,AcceptAsync,ConnectAsync)
1.类 (1)socket IO操作内存管理类 BufferManager // This class creates a single large buffer which can be divid ...