Javascript Promise对象学习

Posted 袜子破了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript Promise对象学习相关的知识,希望对你有一定的参考价值。

 

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);
}));

  

 

 

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对象学习的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 标准内置对象Promise使用学习总结

VSCode自定义代码片段12——JavaScript的Promise对象

VSCode自定义代码片段12——JavaScript的Promise对象

JavaScript - Promise对象

JavaScript学习--Item34 大白话讲解Promise

javaScript-promise