Home  >  Article  >  Web Front-end  >  Promise code example analysis in ES6

Promise code example analysis in ES6

小云云
小云云Original
2018-01-26 16:32:571275browse

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(&#39;200 OK&#39;);}
    if(a>2){reject(&#39;400 died&#39;);}
  },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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn