promise实现

一个最基础版本的实现,其中还没有实现返回promise对象等功能。


		function YPromise(fn){
			var that=this;//保证this指向
			 that.state="pending";
			 that.successList= [];
			 that.failList= [];
			 
			 fn(resolve,reject);
			  function resolve(res){ // 成功的方法
			     setTimeout(function(){
					 that.status = 'fullfilled';
					 that.successList.forEach(function(item,i){
					 					  item(res);
					 })
				 })

				  
	           }
			   function reject(res){ //失败的方法
			       setTimeout(function(){
					   that.state = "rejected";
					   that.failList.forEach(function(item,i){
					   					  item(res);
					   })
				   })
			        
			    }
			     
			   }
			   
            YPromise.prototype.then=function(success,fail){  //回调函数的改变
				   this.successList.push(success);
				   this.failList.push(fail);
			   }
			  YPromise.prototype.catch=function(fail){
				   this.failList.push(fail);
			   }

			var p1=new YPromise(function(resolve,reject){
					if(true){
						resolve("成功")
					}else{
						reject("失败")
					}
				})
			
			  
			  p1.then(function(res){
				  console.log(res);
			  })

Promise.all实现:

			//方法在原型上定义,all方法接受一组可迭代对象作为参数,通常是一组promise
			promise.prototype.all = function(promises){
			//获取参数数组长度,即并发promise的个数。
			    var count = promises.length;
			//保存当前this
			    var that = this;
			//声明结果数组,存放参数数组的执行结果
			    var results = [];
			//forEach循环是并发执行,Promise.all()可以处理并发请求的根源。
			    promises.forEach(function(promise,i){
			//resolve()
			        promise.then(function(data){
			//当每一个promise被处理,count-1
			            count--;
			//传入resolve函数的参数data为与异步操作相关的附加数据。
			            results[i] = data;
			//当参数数组中的所有promise都被处理时。promise.all()返回的promise被解决。
			            if(count == 0){
			                that.resolve(results);
			            }
			//如果某一个失败,promise.all()立即执行reject回调。
			//但剩余的promise依旧继续执行,只不过对promise.all的结果不会产生影响了。
			        },function(err){
			            that.reject(err);
			        });
			    }) ;
			    return this.promise;
			}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Paperback: 280 pages Publisher: Packt Publishing - ebooks Account (February 1, 2016) Language: English ISBN-10: 1785280082 ISBN-13: 978-1785280085 Key Features Learn to use the facilities provided by D3.js to create data-driven visualizations Explore the concepts of D3.js through examples that enable you to quickly create visualizations including charts, network diagrams, and maps Get practical examples of visualizations using real-world data sets that show you how to use D3.js to visualize and interact with information to glean its underlying meaning Book Description This book will take you through all the concepts of D3.js starting with the most basic ones and progressively building on them in each chapter to expand your knowledge of D3.js. Starting with obtaining D3.js and creating simple data bindings to non-graphical HTML elements, you will then master the creation of graphical elements from data. You'll discover how to combine those elements into simple visualizations such as bar, line, and scatter charts, as well as more elaborate visualizations such as network diagrams, Sankey diagrams, maps, and choreopleths. Using practical examples provided, you will quickly get to grips with the features of D3.js and use this learning to create your own spectacular data visualizations with D3.js. What you will learn Install and use D3.js to create HTML elements within the document Use development tools such as JSBIN and Chrome Developer Tools to create D3.js applications Retrieve JSON data and use D3.js selections and data binding to create visual elements from data Create and style graphical elements such as circles, ellipses, rectangles, lines, paths, and text using SVG Turn your data into bar and scatter charts, and add margins, axes, labels, and legends Use D3.js generators to perform the magic of creating complex visualizations from data Add interactivity to your visualizations, including tool-tips, sorting, hover-to-highlight, and grouping and dragging of visuals

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值