三种状态:
- pending
- fulfilled
- rejected
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>my</title>
</head>
<body>
<p>test my</p>
</body>
<script type="module">
import MyPromise from './promise.js';
// 三种状态 1.pending 2.fulfilled 3.rejected
let myP = new MyPromise((resolve,reject)=>{
// resolve("suc");
reject("err");
});
console.log(myP);
</script>
</html>
promise.js:
export default class MyPromise{
constructor(handle){
this.state = "pending";
this.result = undefined;
// handle(function(val){
// // resolve
// this.state = "fulfilled";
// this.result = val;
// }.bind(this),function(val){
// // rejected
// this.state = "rejected";
// this.result = val;
// }.bind(this));
// handle((val)=>{
// // resolve
// this.state = "fulfilled";
// this.result = val;
// },(val)=>{
// // rejected
// this.state = "rejected";
// this.result = val;
// });
handle(this._resolve.bind(this),this._reject.bind(this));
};
_resolve(val){
this.state = "fulfilled";
this.result = val;
};
_reject(val){
this.state = "rejected";
this.result = val;
};
}
打印结果
网友评论