Home >Web Front-end >JS Tutorial >Promise code example analysis in ES6
This article mainly shares with you the analysis of Promise code examples in ES6. I hope you can better understand the usage of promise. Interested friends can refer to it. I hope it can help everyone.
Without further ado, I will post the code directly for you. The details are as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> var a=0 var test=function(resolve, reject){ setTimeout(function(){ a=4; console.log(a) if(a<2){resolve('200 OK');} if(a>2){reject('400 died');} },3000) } var p1=new Promise(test); p1.then(function(resolve){ console.log(resolve) },function(reject){ console.log(reject); }) </script> </body> </html>
Related recommendations:
How to use jQuery's Promise correctly
Asynchronous programming in es6 Promise explanation
ES6 Promise brief introduction
The above is the detailed content of Promise code example analysis in ES6. For more information, please follow other related articles on the PHP Chinese website!