ES6中的Promise对象

var p = new Promise(function(resolve, reject){
window.setTimeout(function(){
console.log("1",1);
resolve(1);
},10);
});
p.then(function(a){
return new Promise(function(resolve,reject){
window.setTimeout(function(){
console.log("2",a+1);
resolve(a + 1);
},10);
});
}).then(function(a){
return new Promise(function(resolve,reject){
window.setTimeout(function(){
console.log("3",a+1);
resolve(a + 1);
},10);
});
}).then((a)=>new Promise((resolve,reject)=>{
window.setTimeout(function(){
console.log("52222",a+1);
resolve(a + 1);
},10);
}));

  

for循环中的ajax请求依次执行,而不是无序执行。

(function () {

    var ajax = function(callback){
window.setTimeout(function(){
callback(new Date());
},1000);
}; var promiseAjax = function(num){
return function (pre){
return new Promise(function (resolve, reject) {
ajax(function (d) {
console.log(num,d);
resolve(d);
});
});
}
} ; var p = promiseAjax(-1)();
for (var i = 0; i < 10; i++) {
p = p.then(promiseAjax(i));
} })();

  

Angular中的$q对象

    var $q = {};
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function(a){
return a + "222"
}).then(function(a){
var deferred = $q.defer();
window.setTimeout(function(){
deferred.resolve("333");
},100);
return deferred.promise;
});
deferred.resolve("1111");

  

var deferred = $q.defer()

...(and then from console inspection)...

$q: Object {defer: function, reject: function, when: function, all: function}

deferred: Object {resolve: function, reject: function, notify: function, promise: Object}

deferred.promise: Object {then: function, catch: function, finally: function}
$q.reject(reason) returns a rejected promise with the reason passed as argument and defered. Reject rejects an existent defered whether its process has finished or not.

  

try、catch、throw和then、catch、reject

Javascript Promise对象学习的更多相关文章

  1. JS Promise对象学习

    Promise对象的三个状态 pending(进行中) fulfilled(已成功) rejected(已失败) Promise代表一个异步操作,对象的状态一旦改变,就不会再改变 Promise构造函 ...

  2. [javascript] Promise简单学习使用

    原文地址:http://www.cnblogs.com/dojo-lzz/p/4340897.html 解决回调函数嵌套太深,并行逻辑必须串行执行,一个Promise代表一个异步操作的最终结果,跟Pr ...

  3. ES6之Promise对象学习——8个例子学会Promise

    目录 Promise 立即执行 Promise 三种状态 Promise 不可逆性 链式调用 Promise.then()回调异步性 Promise中的异常 Promise.resolve() res ...

  4. JavaScript - Promise对象

    优点: 在异步执行的流程中,把执行代码和处理结果的代码清晰地分离(因为Promise对象有链式写法,有then和catch) 组合使用Promise,就可以把很多异步任务以并行和串行的方式组合起来执行 ...

  5. JavaScript之对象学习

    对象是一种非常重要的数据类型,他是一种自包含的数据集合,包含在对象里面的数据可以通过属性和方法两种形式来访问; 1.属性是隶属于某个特定对象的变量; 2.方法是只有某个特定对象才能调用的函数; 而对象 ...

  6. JavaScript学习系列3 -- JavaScript arguments对象学习

    在实际项目开发中,目前还是很少使用到JavaScript 中的arguments对象,那么它到底是干什么用的呢 arguments是JavaScript中的一个类数组对象,它代表传给一个正在执行的函数 ...

  7. javascript中对象学习

    第一篇文章: javascript中this关键字的详细解析:   http://blog.csdn.net/wyj880220/article/details/7305952 Javascript ...

  8. JavaScript Promise的学习笔记

    首先声明:本人今天刚接触Promise,通过一个例子,希望能更好的来理解,如果有不对的地方,还望指正 Promise是专门为解决 js中回调而引起的各种问题,而产生的. 在异步编程中,我们经常使用回调 ...

  9. 第一百二十节,JavaScript事件对象

    JavaScript事件对象 学习要点: 1.事件对象 2.鼠标事件 3.键盘事件 4.W3C与IE JavaScript事件的一个重要方面是它们拥有一些相对一致的特点,可以给你的开发提供更多的强大功 ...

随机推荐

  1. Android开发--RadioButton的应用

    1.简介 RadioButton为单选按钮,当一个按钮被选中后,点击其他按钮无法使上一个按钮变为未选中状态.RadioGroup是可以容纳多个RadioButton的容器, 通过使用RadioGrou ...

  2. PHP数组去重..............过滤字段

    $test_data = M('hot'); //实例化数据表 $data = $test_data->Distinct(true)->field('descriprion')->o ...

  3. [转]CSS网页布局:div水平居中的各种方法

    http://jingyan.baidu.com/article/fa4125ac90a2a328ac70929e.html 在Web标准中的页面布局是使用Div配合CSS来实现的.这其中最常用到的就 ...

  4. bzoj 2878: [Noi2012]迷失游乐园

    #include<iostream> #include<cstring> #include<cstdio> #define M 100005 #define ld ...

  5. Oracle数据库中scott用户不存在的解决方法

    SCOTT用户是我们学习Oracle过程中一个非常重要的实验对象,在我们建立数据库的时候,如果是选择定制模式的话,SCOTT用户是不会默认出现的,不过我们可以通过使用几个简单命令来使这个用户出现.以下 ...

  6. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  7. android贪吃蛇(超级简陋版)

    public class body { public int ax;//代表X周变量 public int ay;//代表Y轴变量 public int getAx() { return ax; } ...

  8. C++ 之 const 随笔记

    const关键字,相信对C语言有所了解的同学都应该知道他的作用:1.修饰常量,2.修饰指针,3.修饰函数 1.修饰常量 const修饰后的变量被定义为常量 2.修饰指针 当用const修饰指针的时候, ...

  9. UIImagePickerController 获取相片视频

    1.UIImagePickerController的静态方法: imagepicker = [[UIImagePickerController alloc]init];    //UIImagePic ...

  10. div嵌套引起的margin-top不起作用

    通常大家在制作网页的过程中会遇到很多棘手的问题,比如我在写一个页面的时候,遇到了div嵌套引起的margin-top不起作用,对内部的div设置margin-top时,内部对于外部的div并没有产生一 ...