一眼看懂promise async的区别
// promise方法
let p1 = new Promise((resolve,reject) => {
setTimeout(() => {
resolve('我是p1')
},4000)
})
let p2 = new Promise((resolve,reject) => {
setTimeout(() => {
resolve('我是p2')
},200)
})
let p3 = new Promise((resolve,reject) => {
setTimeout(() => {
resolve('我是p3')
},200)
})
// 想让p1完成后再执行P2再执行P3
// 数量太多只能循环嵌套
p1.then((res) => {
console.log(res);
p2.then((res) => {
console.log(res);
p3.then((res) => {
console.log(res);
})
})
})
// async await语法糖
let a1=()=>{
return new Promise((resolve,reject) => {
setTimeout(() => {
resolve('我是a1')
},4000)
})
}
let a2=()=>{
return new Promise((resolve,reject) => {
setTimeout(() => {
resolve('我是a2')
},40)
})
}
let a3=()=>{
return new Promise((resolve,reject) => {
setTimeout(() => {
resolve('我是a3')
},40)
})
}
// 想让a1完成后再执行a2再执行a3
//能避免回调
async function asy(){
await a1().then((res) => {console.log(res)});
await a2().then((res) => {console.log(res)});
await a3().then((res) => {console.log(res)});
}
asy();
一眼看懂promise async的区别的更多相关文章
- require和require.async的区别
本文用seajs来讲解两种模块加载方式require和require.async的区别,类似java里的import,php里的include. <!DOCTYPE html> <h ...
- <script>标签里的defer和async属性 区别(待补充)
defer与async的区别(表格显示): table th:first-of-type { width: 150px; } table th:nth-of-type(2) { } 区别 defer ...
- 理解Promise.all,Promise.all与Promise.race的区别,如何让Promise.all在rejected失败后依然返回resolved成功结果
壹 ❀ 引 我在 es6入门4--promise详解 这篇文章中有详细介绍Promise对象的用法,文章主题更偏向于对于Promise概念的理解与各方法基本使用介绍:而世上一个比较有趣的问题就是,即 ...
- vue使用技巧:Promise + async + await 解决组件间串行编程问题
业务场景描述 大家都通过互联网投递过简历,比如在智联.58.猎聘等平台.投递心仪的职位前一般都需要前提创建一份简历,简历编辑界面常规的布局最上面是用户的个人基本信息,如姓名.性别.年龄.名族等,接着是 ...
- url,href和src的区别,defer和async的区别
URL(Uniform Resource Locator):统一资源定位符,互联网上的每个文件都有一个唯一的URL,基本URL包含协议,IP地址,路径和文件名. 重点:herf和src的区别 href ...
- angular2 学习笔记 ( Rxjs, Promise, Async/Await 的区别 )
Promise 是 ES 6 Async/Await 是 ES 7 Rxjs 是一个 js 库 在使用 angular 时,你会经常看见这 3 个东西. 它们都和异步编程有关,有些情况下你会觉得用它们 ...
- promise async await使用
1.Promise (名字含义:promise为承诺,表示其他手段无法改变) Promise 对象代表一个异步操作,其不受外界影响,有三种状态: Pending(进行中.未完成的) Resolved( ...
- 【一起来烧脑】读懂Promise知识体系
知识体系 Promise基础语法,如何处理错误,简单介绍异步函数 内容 错误处理的两种方式: reject('错误信息').then(null, message => {}) throw new ...
- 一道题理解setTimeout,Promise,async/await以及宏任务与微任务
今天看到这样一道面试题: //请写出输出内容 async function async1() { console.log('async1 start'); await async2(); consol ...
随机推荐
- centOS7 安装vsftp服务器
一.目的:有许多时候我们需要从自己机器上,上传文件到Linux服务器上,想要上传文件就必须要通过FTP 协议(File Transfer Protocol(文件传输协议)).所以要在服务器上配置FTP ...
- Permutations II 去掉重复的全排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LintCode_408 二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示). 思路 string s = ""; 目标字符串 cp 存储进位;取 0或1 sum = a[i] + b[i] + cp;分为 ...
- 【JZOJ4742】【NOIP2016提高A组模拟9.2】单峰
题目描述 输入 输出 样例输入 2 样例输出 2 数据范围 解法 答案为2^(n-1),快速幂即可. 证明:显然峰值必定为n,那么对于其他n-1个数,要么放在峰值的左边,要么放在峰值的右边,所以方案数 ...
- 洛谷P2607 骑士
题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里, ...
- GDB调试命令手册
使用GDB 启动 $ gdb program # program是你的可执行文件,一般在当前目录 $ gdb program core # gdb同时调试运行程序和cor ...
- @codeforces - 718E@ Matvey's Birthday
目录 @description@ @solution@ @accepted code@ @detail@ @description@ 给定一个长度为 n 的字符串 s,保证只包含前 8 个小写字母 ' ...
- 2019-8-31-dotnet-使用-lz4net-压缩-Stream-或文件
title author date CreateTime categories dotnet 使用 lz4net 压缩 Stream 或文件 lindexi 2019-08-31 16:55:58 + ...
- python 检测文件夹的数据变动
from watchdog.observers import Observerfrom watchdog.events import *import time class FileEventHandl ...
- Jmeter json处理器