JavaScript study notes--Promise


1.what is Promise?

     "promise" means God.He knows what will happened in the future.It represents a value that may not be available yet,but will be resolved at some point in future.It allows you write a asnychronous process in a snychronous fashion.For instance,If you use Promise API to call an remote webservice ,you will create a promise object represent the data that will be returned from the server.Promise object just like a proxy for that and will be available as soon as the request back.

     I think Promise is a  pattern. It provide a mechanism which can gurantee the requests be dealed in some sort of sequence.At the same time,It has a ordered and gentle way to process the exception and errors.

2.How to code?

  the following code is the normal form :

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

    //if satisfy some condition,execute successfully,then invoke the resolve function

    resolve(resultObj);

  //if the error or exception occurs( or satisfy some condition) during the execution ,invoke the reject method

   reject(new Error());

});

promise.then(success,failed);

   the previous segment of code will return the resultObj,the resultObje will be passed to the success function as an argument,but the condition is that the  "then" has been invoked.it is the same to the falied,only when the promise failed,the new Error () object will been passed to failed function as an argument.

3.why do you choose it?

    It will make the process more clearly,and the code be more readable and  in accord with the way of promiser`s thinking.

     This reference page(http://www.html5rocks.com/zh/tutorials/es6/promises/#toc-api) will tell you the advanteages of using it .and this two pages (

http://www.sitepoint.com/overview-javascript-promises/  ; 

http://www.zhangxinxu.com/wordpress/2014/02/es6-javascript-promise-%E6%84%9F%E6%80%A7%E8%AE%A4%E7%9F%A5/) are very understandability.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值