GithubHelp home page GithubHelp logo

swift-promise's Introduction

swift-promise

====================

Build Status Coverage Status

一个Promise A+规范的实现,promise的介绍,请 点击

详细的实现原理

本实现完全遵循Promise A+规范,全部实现Promise A+测试用例

运行npm test可看到效果

Usage使用方法


var getJSON = function(url) {
  var promise = new Promise(function(resolve, reject){
    var client = new XMLHttpRequest();
    client.open("GET", url);
    client.onreadystatechange = handler;
    client.responseType = "json";
    client.setRequestHeader("Accept", "application/json");
    client.send();

    function handler() {
      if (this.readyState === this.DONE) {
        if (this.status === 200) { resolve(this.response); }
        else { reject(this); }
      }
    };
  });

  return promise;
};

getJSON("/posts.json").then(function(json) {
  // continue
}, function(error) {
  // handle errors
});

链式调用Chaining

var promise = new Promise(function (resolve, reject) {
        setTimeout(function(){
            resolve(100);
        },2000)
})

promise.then(function fn1(val){
    return val+100
}).then(function fn2(val){
    alert(val);
})

错误处理Error Handling


var promise = new Promise(function (resolve, reject) {

        setTimeout(function(){
            resolve(100);
        },2000)

})

promise.then(function fn1(val){
    return val+100
}).then(function fn2(val){
    alert(val);
}).catch(function(reason){
    
});
    

finally实例方法

无论promise兑现还是拒绝,都会执行,使用场景如某些ajax请求的loading界面最终会结束

var promise = new Promise(function (resolve, reject) {
        setTimeout(function(){
            resolve(100);
        },2000)

    })

promise.then(function fn1(val){
    return val+500;
})
.finally(function(result){ //result可能是履行的值或者抛出的异常
    //TODO
})
    

ES6中静态方法

Promise.resolve


Promise.resolve("abc")
    .then(function (val) {
        //val 为"abc"            
    })

            
var p = new Promise(function (resolve) {
    resolve("thenable");  
})

Promise
    .resolve(p)
    .then(function (val) {
      //val 为"thenable"            
    })
           

Promise.reject

Promise
    .reject("abc")
    .then(function (val) {}, function (reason) {
        alert(reason)//为"abc"
    })
    

Promise.all


var p = new Promise(function(resolve, reject) { resolve(3); });

Promise.all([true, p]).then(function(values) {
  // values == [ true, 3 ]
});

Promise.race


var p1 = new Promise(function(resolve, reject) { setTimeout(resolve, 500, "one"); });
var p2 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, "two"); });

Promise.race([p1, p2]).then(function(value) {
  // value === "two"
});

var p3 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, "three"); });
var p4 = new Promise(function(resolve, reject) { setTimeout(reject, 500, "four"); });

Promise.race([p3, p4]).then(function(value) {
  // value === "three"               
}, function(reason) {
  // Not called
});

var p5 = new Promise(function(resolve, reject) { setTimeout(resolve, 500, "five"); });
var p6 = new Promise(function(resolve, reject) { setTimeout(reject, 100, "six"); });

Promise.race([p5, p6]).then(function(value) {
  // Not called              
}, function(reason) {
  // reason === "six"
});

swift-promise's People

Contributors

guilipan avatar

Watchers

James Cloos avatar kellan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.