Giter VIP home page Giter VIP logo

promise's Introduction

自己实现的promise 小轮子

目前支持的功能包括:

  • 简单的resolve, reject方法;
  • 链式调用加

使用的范例

new FuPromise(function(resolve){
   window.setTimeout(function(){resolve(10000)}, 4000);
}).then(function(value){
  console.log(value);
  return new FuPromise(function(resolve, reject) {
    window.setTimeout(function(){
      resolve(20000)
    }, 4000);
  });
}).then(function(val){
  console.log(val + 'dfa');
  return new FuPromise(function(resolve,reject) {
    window.setTimeout(function(){
      reject(val + 'ddd');
    }, 1000);
  })
}).then(function(value){
  console.log(value);
}).catch(function(error){
  console.log(error);
});

如上代码支持链式写法,catch的链式捕捉

  • Promise的all方法使用范例:
var promise1 = new FuPromise(function(resolve, reject){
  window.setTimeout(function(){resolve('promise1')}, 1000);
});
var promise2 = new FuPromise(function(resolve, reject){
  window.setTimeout(function() {
    resolve('promise2')
  }, 2000);
});
FuPromise.all([promise1, promise2]).then(function(results){
  console.log(results);
});
  • Promise的race 方法使用范例:
var promise1 = new FuPromise(function(resolve, reject){
  window.setTimeout(function(){resolve('promise1')}, 1000);
});
var promise2 = new FuPromise(function(resolve, reject){
  window.setTimeout(function() {
    resolve('promise2')
  }, 2000);
});
FuPromise.race([promise1, promise2]).then(function(results){
  console.log(results);
});

ajax的请求范例

function getURL(URL) {
    return new FuPromise(function (resolve, reject) {
        var req = new XMLHttpRequest();
        req.open('GET', URL, true);
        req.onload = function () {
            if (req.status === 200) {
                resolve(req.responseText);
            } else {
                reject(new Error(req.statusText));
            }
        };
        req.onerror = function () {
            reject(new Error(req.statusText));
        };
        req.send();
    });
}
// 运行示例
var URL = "http://www.fengjr.com/";
getURL(URL).then(function (value){
    console.log('value');
    console.log(value);
}).catch(function (error){
    console.error(error);
});

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.