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

时间: 2024-08-11 03:20:55

promise链式的相关文章

Promise链式回调的使用

/*Promise通常配合then方法来链式的使用,then方法里面第一个回调函数表示成功状态,也就是resolve,第二个是失败状态-reject,如果默认写一个参数的话,默认resolve*/ let checkLogin=()=> { return new Promise((resolve,reject)=>{ let flag=document.cookie.indexOf("userId")!=-1?true:false; if(flag=true){ resol

promise链式调用

//不了解promise可以先去看看,了解后再来相信大家读能看的懂function Promise1(resolve, reject) { setTimeout(function() { resolve('1'); },5000);} function Promise2(resolve, reject) { setTimeout(function() { resolve('2'); },5000);} function Promise3(resolve) { setTimeout(functio

jQuery的链式调用原理,Promise的链式调用,this的问题

最近被问到这个问题,jq的链式调用原理,当时比较懵=.=,毕竟现在jq接触的机会变很少了. jq的链式调用 jq的链式调用其实就是比如我们在选择dom的时候, $('input[type="button"]') .eq(0).click(function() { alert('点击我!'); }).end().eq(1) .click(function() { $('input[type="button"]:eq(0)').trigger('click'); }).

Promise.then链式调用

let a = new Promise((resolve,reject)=>{ resolve(1) }).then((r)=>{console.log(r)}).then(()=>{console.log(2)}).then(()=>{consol.log(3)}) // 1 // 2 // 3 let b = new Promise((resolve,reject)=>{resolve(1)}).then((r)=>{  return 3}).then((r)=&g

异步链式编程—promise沉思录

一.promise的组成 1.task:promise要完成的任务: 2.result:处理完的数据: 3.status:状态: 4.fulfill.reject(对应catch) 5.ResolveCallback ErrorCallback promise状态的解释函数 6.resolve: 对promise当前的状态作出解释,已完成的状态立即执行回掉,未完成的状态注册回掉函数: 7.then:前一promise的回掉注册,后一promise的前导: 二.promose状态机: fulfil

面向对象的链式调用

1. 对象的链式调用 function Chain(){ this.n=0;//属性不一定一开始的时候全部都要初始化 this.fn1=function(_obj){//this指向  new Chain()实例化的对象 alert(this.n++);//注意:alert(this.n++)与this.fn1中的this 不一定指向的对象是一样的 return this; } this.fn2=function(){//同上 alert(this.n++);//注意:alert(this.n+

jQuery插件编写及链式编程模型小结

JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我们就来看看如何把我们常用的功能做出JQuery插件,然后像使用jQuery那样来操作DOM.  一.jQuery插件开发快速上手 1.jQuery插件模板 关于jQuery插件的编写,我们可以通过为jQuery.fn增加一个新的函数来编写jQuery插件.属性的名字就是你的插件的名字,其模板如下: (function($){ $.fn.m

jQuery插件编写及链式编程模型

jQuery插件编写及链式编程模型小结 JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我们就来看看如何把我们常用的功能做出JQuery插件,然后像使用jQuery那样来操作DOM.  一.jQuery插件开发快速上手 1.jQuery插件模板 关于jQuery插件的编写,我们可以通过为jQuery.fn增加一个新的函数来编写jQuery插件.属性的名字就是你的插件的名字,其模板如下:

JavaScript 链式结构序列化详解

一.概述 在JavaScript中,链式模式代码,太多太多,如下: if_else: if(...){ //TODO }else if(...){ //TODO }else{ //TODO } switch: switch(name){ case ...:{ //TODO break; } case ...:{ //TODO break; } default:{ //TODO } } 疑问:诸如上述这些链式代码,倘若,我们想将其扁平化链式处理呢?如下: //fn1,f2,f3为处理函数 _if(