ES6——异步操作之Promise
基本概念:
Promise : 是 ES6 中新增的异步编程解决方案,提现在代码中他是一个对象 可以通过Promise构造函数来实例化。
-new Promise(cb) ===> 实例的基本使用,Pending Resolved Rejected
> 两个原型方法:
-Promise.prototype.then() -Promise.prototype.catch()
> 两个常用的 静态方法。
-Promise.all(); -Promise.resolve();
conse imgs=[ "http://i1.piimg.com/1949/4f411ed22ce88950.jpg", "http://i1.piimg.com/1949/5a35e8c2b246ba6f.jpg", "http://i1.piimg.com/1949/1afc870a86dfec5f.jpg" ];
//new Promise(cb);
//Pending(进行中)===>Resolved(已完成)
//Pending(进行中)===>Rejected(已失效)
const p = new Promise(function(resolve,reject){
const img = new Image();
img.onload=function(){
resolve(this);
};
img.onerror=function(err){
reject(new Error("图片加载失败"));
};
})
console.log(123);
p.then(function(img){
console.log("加载完成");
document.body.appendChild(img);
}).catch(function(err){
console.log(err);
});
console.log(456);
//////////////封装函数
function loadImg(url){
const p = new Promise(function(resolve,reject){
const img = new Image();
img.onload=function(){
resolve(this);
};
img.onerror=function(err){
reject(new Error("图片加载失败"));
};
});
return p;
}
loadImg(imgs[0]).then(function(img){
document.body.appendChild(img);
})
///////Promise.all() 可将多个 Promise实例包装成一个新的Promise实例。
const allDone=Promise.all([loadImg(imgs[0]),loadImg(imgs[1]),loadImg(imgs[2]),loadImg(imgs[""])]);
allDone.then(function(datas){
datas.forEach(function(item,i){
document.body.appendChild(item);
});
}).catch(function(err){
console.log(err);
})
///////Promise.resolve()
Promise.resolve(loadImg(imgs[0])).then(function(img){
document.body.appendChild(img);
})
///////Promise.resolve()
Promise.resolve(loadImg(imgs[0])).then(function(img){
document.body.appendChild(img);
})
以上。
ES6——异步操作之Promise的更多相关文章
- ES6异步操作之Promise
一直以来觉得异步操作在我心头像一团迷雾,可是它重要到我们非学不可,那就把它的面纱解开吧. ES6 诞生以前,异步编程的方法,大概有下面四种. 回调函数 事件监听 发布/订阅 Promise 对象 异步 ...
- es6中的promise对象
Promise是异步里面的一种解决方案,解决了回调嵌套的问题,es6将其进行了语言标准,同意了用法,提供了`promise`对象, promise对象有三种状态:pending(进行中) .Resol ...
- 深入理解 JavaScript 异步系列(3)—— ES6 中的 Promise
第一部分,Promise 加入 ES6 标准 原文地址 http://www.cnblogs.com/wangfupeng1988/p/6515855.html 未经作者允许不得转载! 从 jquer ...
- ES6中的Promise用法
Node的产生,大大推动了Javascript这门语言在服务端的发展,使得前端人员可以以很低的门槛转向后端开发. 当然,这并不代表迸发成了全栈.全栈的技能很集中,绝不仅仅是前端会写一些HTML和一些交 ...
- 异步操作之 Promise 和 Async await 用法进阶
ES6 提供的 Promise 方法和 ES7 提供的 Async/Await 语法糖都可以更好解决多层回调问题, 详细用法可参考:https://www.cnblogs.com/cckui/p/99 ...
- vuex+Es6语法补充-Promise
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式,采用 集中式存储管理 单页面的状态管理/多页面状态管理 使用步骤: // 1.导入 import Vuex from 'vuex' // ...
- ES6异步操作Promise
什么是Promise Promise是异步编程的一种解决方案,说白了就是一个构造函数,带有all,reject,resolve这几个方法,圆形上有then,catch等方法 Promise的特点 对象 ...
- ES6 - Note5:Promise
1.Promise介绍 Promise最早是社区提出和实现,后面ES6将其写入标准,并原生提供Promise对象,是一种异步编程的解决方案,具体的概念大家可以去查看相关的资料.传统上处理异步都是以ca ...
- es6面试问题——Promise
话说刚换工作一个月有余,在上家公司干的实在是不开心,然后就出来抱着试试的心态出来面了几家公司,大多数公司问的前端问题也就那么多,其中有个面试问题让我记忆犹新,只因为没有答上来,哈哈! 当时面试官问我怎 ...
随机推荐
- selenium webdriver——JS滚动到指定位置
1.DOM滚动方法 1.scrollIntoView(alignWithTop) 滚动浏览器窗口或容器元素,以便在当前视窗的可见范围看见当前元素.如果alignWithTop为true,或者省略它, ...
- Spring启动时获取自定义注解的属性值
1.自定义注解 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- Readme.MD 例子
了解一个项目,恐怕首先都是通过其Readme文件了解信息.如果你以为Readme文件都是随便写写的那你就错了.github,oschina git gitcafe的代码托管平台上的项目的Readme. ...
- Maven 快速构建一个项目
参考:http://www.spring4all.com/article/266 mvn archetype:generate -DgroupId=springboot -DartifactId=sp ...
- 在coursera上有哪些值得推荐的课程
来自知乎 https://www.zhihu.com/question/22436320/answer/224996328
- 【333】Python3.6 格式化文本
看如下例子进行体会: min_temperature = 0 max_temperature = 300 step = 20 # \t: A tab print('Fahrenheit\tCelsiu ...
- 在Ubuntu16.04中安装Docker CE
apt-get install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https ...
- Linux-CentOS 更新Firefox版本
1.用你本地的旧版 firefox,访问http://www.firefox.com.cn,下载Linux版本的Firefox. 2.进入存放下载文件(Firefox-latest-x86_64.ta ...
- Linux安装centos7
安装 选择安装centos7,按回车 进入到安装界面: 选择我要自定义分区,然后点击左上角done: 然后自定义分区(swap分区一般为内存的2倍,我这里用的虚拟机截的图,所以内存给的少,具体按照自己 ...
- RedHat6使用centos6的yum源
更换源 cd /etc/yum.repos.d/ wget http://mirrors.163.com/.help/CentOS6-Base-163.repo vi CentOS6-Base-.re ...