Promise 是用来执行异步操作的。

但有时一个异步操作需要等其他的异步操作完成,这时候就可以使用then来做。

 function loadImageAsync(url) {
return new Promise(function(resolve, reject) {
var image = new Image(); image.onload = function() {
console.log("load");
resolve(image);
}; image.onerror = function() {
reject(new Error('Could not load image at ' + url));
};
console.log("change src");
function init_img() {
image.src = url;
}
setTimeout(init_img,2000);
});
}

loadImageAsync 用来异步加载图片. setTimeout 人为地延迟2秒加载

function loadImageAsync2(url) {
return new Promise(function(resolve, reject) {
var image = new Image(); image.onload = function() {
console.log("2load");
resolve(image);
}; image.onerror = function() {
reject(new Error('Could not load image at ' + url));
};
console.log("2change src");
function init_img() {
image.src = url;
}
setTimeout(init_img,1000);
});
}
loadImageAsync2 也是用来加载图片,人为地延迟1秒。
var p = loadImageAsync('http://img02.tooopen.com/images/20141231/sy_78327074576.jpg');
p.then(function (img) {
console.log("1:"+img.src);
});
var h = loadImageAsync2('http://img02.tooopen.com/images/20141225/sy_77944235469.jpg');
h.then(function (img) {
console.log("2:"+img.src);
});
console.log("jjjjj");
 

第一种调用方式,分别创建promise 实例 p 和 h。

调用结果:


可以看到 图片2先加载, 然后加载图片1 。
  var s = loadImageAsync('http://img02.tooopen.com/images/20141231/sy_78327074576.jpg');
s.then(function (img) {
console.log("1:"+img.src);
return loadImageAsync2('http://img02.tooopen.com/images/20141225/sy_77944235469.jpg');
}).then(function (img) {
console.log("2:"+img.src);
});

创建新的promise 对象 s. 我们在s 的then 成功回调函数中 去调用 loadImageAsync2 函数。

这样就保证了 加载图片2 之前先加载图片1.

这里注意的是第二个then 其实是loadImageAsync2 返回的promise 实例调用的。

Promise相关学习链接:http://es6.ruanyifeng.com/#docs/promise

 

Promise 异步执行的同步操作的更多相关文章

  1. asap异步执行实现原理

    目录 为什么分析asap asap概述 asap源码解析-Node版 参考 1.为什么分析asap 在之前的文章 async和await是如何实现异步编程? 中的浅谈Promise如何实现异步执行小节 ...

  2. Promise then中回调为什么是异步执行?Promise执行机制问题

    今天发现一个问题,看下方代码 let p = new Promise(function(resolve, reject) { resolve() console.log('); }); p.then( ...

  3. 学习Promise异步编程

    JavaScript引擎建立在单线程事件循环的概念上.单线程( Single-threaded )意味着同一时刻只能执行一段代码.所以引擎无须留意那些"可能"运行的代码.代码会被放 ...

  4. 超耐心地毯式分析,来试试这道看似简单但暗藏玄机的Promise顺序执行题

    壹 ❀ 引 就在昨天,与朋友聊到JS基础时,她突然想起之前在面试时,遇到了一道难以理解的Promise执行顺序题.由于我之前专门写过手写promise的文章,对于部分原理也还算了解,出于兴趣我便要了这 ...

  5. Js中强大的Promise异步机制

    少年别激动 我的这份随笔里面只涉及promise概念 如果想深入了解Promise的用法 可以去阮老师es6入门里面详读 奉上链接 http://es6.ruanyifeng.com/#docs/pr ...

  6. ES6笔记(7)-- Promise异步编程

    系列文章 -- ES6笔记系列 很久很久以前,在做Node.js聊天室,使用MongoDB数据服务的时候就遇到了多重回调嵌套导致代码混乱的问题. JS异步编程有利有弊,Promise的出现,改善了这一 ...

  7. 【ES6】Generator+Promise异步编程

    一.概念 首先我们要理解Generator和Promise的概念. Generator:意思是生成器,可以在函数内部通过yeild来控制语句的执行或暂停状态. *Foo(){ yeild consol ...

  8. 8张图让你一步步看清 async/await 和 promise 的执行顺序

    摘要: 面试必问 原文:8张图帮你一步步看清 async/await 和 promise 的执行顺序 作者:ziwei3749 Fundebug经授权转载,版权归原作者所有. 为什么写这篇文章? 说实 ...

  9. 8 张图帮你一步步看清 async/await 和 promise 的执行顺序(转)

    https://mp.weixin.qq.com/s?__biz=MzAxODE2MjM1MA==&mid=2651555491&idx=1&sn=73779f84c289d9 ...

随机推荐

  1. sql获取每门课程成绩最好的学生信息

    1.相关数据表 Score表 [User]表 SQL语句例如以下: --查询出各科成绩最好的学生信息 --自连接 --SELECT TOP 1 * FROM Score B WHERE B.Score ...

  2. [RxJS] Getting Input Text with Map

    By default, Inputs will push input events into the stream. This lesson shows you how to use map to c ...

  3. c++11 NULL、0、nullptr

      C的NULL 在C语言中,我们使用NULL表示空指针,也就是我们可以写如下代码: int *i = NULL;foo_t *f = NULL; 实际上在C语言中,NULL通常被定义为如下: #de ...

  4. BGP

    http://network.51cto.com/art/200912/172439.htm http://blog.sina.com.cn/s/blog_b457dde80101cyqr.html ...

  5. tomcat配置数据源

    1.修改conf下的context.xml,在<context>标签中添加: <Resource name="jdbc/soa" auth="Conta ...

  6. 普通用户之间的ssh无密码访问设置方法

    两台CentOS6.2服务器,客户端是node1,服务器是node2,先都用root用户配置,方法如下: 第一步:在客户端Node1:生成密匙对,我用的是rsa的密钥.使用命令 "ssh-k ...

  7. easyui treeJson 带层数

    public string GetTreeNav(int ID,int Num) { StringBuilder sb = new StringBuilder(); sb.Append("[ ...

  8. 转 Oracle全文检索http://docs.oracle.com/cd/E11882_01/text.112/e24436/toc.htm

    SQL > exec ctx_ddl.create_preference ('my_test_lexer','chinese_lexer') : PL/SQL 过程成功完成 SQL > E ...

  9. 【Solr初探】Solr安装,启动,查询,索引

    1. 安装&启动 官网:http://lucene.apache.org/solr/ 下载源代码,解压,进入根目录(我把solr放在/usr/local/solr下) 在/usr/local/ ...

  10. 使用MSSM管理工具登录LOCALDB

    调试程序没有安装 sql server时,可以使用localdb.这是一个简易的sql server数据库,用于本地测试是很方便,省去安装SQL SERVER的工作 电脑上安装了VS2013 VS20 ...