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.