测试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了') ...
随机推荐
- C# 语言基础学习路线图
一直以来,对于很多知识点都是存于收藏夹中,随着时间地变更,收藏夹中链接也起来越多,从未进行整理,也很零散,所以想对曾经遇到并使用过的一些知识形成文档,作为个人知识库的一部分. 就从C# 语言基础开始, ...
- [NOIP10.4模拟赛]2.y题解--折半搜索+状压计数
题目链接: 咕 闲扯: 这题暴力分似乎挺多,但是一些奇奇怪怪的细节没注意RE了,还是太菜了 分析: 首先我们考虑最naiive的状压DP ,\(f[u][v][state]\)表示u开头,v结尾是否存 ...
- 【图像处理 】 一、OSTU分割法
图像中像素的灰度值小于阈值T的像素个数记作N0,像素灰度大于阈值T的像素个数记作N1,则有: 图像大小:M*N T为二值化的阈值: N0为灰度小于T的像素的个数,N0的平均灰度为μ0 N1 为灰度大于 ...
- caffe笔记
1. 训练 cifar10 示例 ① cd caffe.1.0.0 ./data/cifar10/get_cifar10.sh #获取图片 ② ./examples/cifar10/cre ...
- oracel数据泵导出导入
Oracle11g 使用数据泵导入/导出数据 expdp/impdp 目标:使用oracle数据泵,将A电脑上的数据库databaseA导出后,再导入到B电脑上的数据库databaseB中. A电脑上 ...
- 【Java并发】线程安全和内存模型
一.概述 1.1 什么是线程安全? 1.2 案例 1.3 线程安全解决办法: 二.synchronized 2.1 概述 2.2 同步代码块 2.3 同步方法 2.4 静态同步函数 2.5 总结 三. ...
- Linux内核移植的若干问题
- 从Windows文件夹到Linux分区
1. 先说几句 如果你, 知道什么是Windows系统 简单使用Windows系统 想要了解或安装Linux 刚接触Linux会有以下疑惑, 什么是分区? 如何建立分区? 怎么安装系统到到D盘? 安装 ...
- Linux rpm和yum软件管理
rpm是管理程序的一个小工具,rpm常来用作查询 什么源码包:大多数都是tar.gz,bz.bz2结尾的包 zip结尾的包 压缩格式为 zip –r 命名.zip ./* 解压格式为 unzip 命名 ...
- jQuery获取上传文件的名称
//获取文件名称 function getFileName(path) { var pos1 = path.lastIndexOf('/'); var pos2 = path.last ...