JavaScript 中的Promise学习
代码示例:
<script type="text/javascript"> new Promise(function(resolve, reject) { console.log(111); resolve(222); }).then(function(value) { console.log(value); return 333; }).then(function(value) { console.log(value); throw "An Error"; }).catch(function(err) { console.log(err); }).finally(function(){ console.log("End"); }); </script>
示例代码2:
<html> <head> <h2> Javascript Promise</h2> </br> </head> <body> <script> var p = new Promise(function(resolve, reject) { var x = 2 + 3; if (x == 5) resolve(" executed and resolved successfully"); else reject("rejected"); }); p.then(function(fromResolve) { document.write("Promise is" + fromResolve); }).catch(function(fromReject) { document.write("Promise is " + fromReject); }); </script> </body> </html>