zoukankan      html  css  js  c++  java
  • promise链式

    var getJSON = function(url) {
      var promise = new RSVP.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

    One of the really awesome features of Promises/A+ promises are that they can be chained together. In other words, the return value of the first resolve handler will be passed to the second resolve handler.

    If you return a regular value, it will be passed, as is, to the next handler.

    getJSON("/posts.json").then(function(json) {
      return json.post;
    }).then(function(post) {
      // proceed
    });

    The really awesome part comes when you return a promise from the first handler:

    getJSON("/post/1.json").then(function(post) {
      // save off post
      return getJSON(post.commentURL);
    }).then(function(comments) {
      // proceed with access to post and comments
    });

    转自: https://github.com/tildeio/rsvp.js

  • 相关阅读:
    MySQL数据库命令行界面不支持中文
    mysqldump使用方法(MySQL数据库的备份与恢复)
    MySQL性能测试初试(1)--sysbench
    composer安装
    Java关键字[static].md
    Docker容器
    Docker概述及安装
    Docker镜像
    定时任务[crontab]
    Linux下的curl工具
  • 原文地址:https://www.cnblogs.com/xjinza/p/4702637.html
Copyright © 2011-2022 走看看