美文网首页
Promise-02 then参数

Promise-02 then参数

作者: 呆桃冲鸭冲鸭 | 来源:发表于2020-09-10 21:00 被阅读0次

then 2个参数 : 1.异步 2.同步

<!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">
    //  type="module" 严格模式 
    import MyPromise from './promise.js';
    // 三种状态 1.pending 2.fulfilled 3.rejected
    let myP = new MyPromise((resolve,reject)=>{
        setTimeout(()=>{
            console.log('====')
            resolve("suc...");
        });
        // reject("err...");
    });
    // console.log(myP);
    
    //then 2个参数 
    myP.then(res=>{
        console.log(res);
    },err=>{
        console.log(err);
    })

</script>
</html>
promise.js:
export default class MyPromise{
    constructor(handle){
        this.state = "pending";
        this.result = undefined;
        this.resolveFn = undefined;
        this.rejectFn = undefined;
        handle(this._resolve.bind(this),this._reject.bind(this));
    };
    _resolve(val){
        this.state = "fulfilled";
        this.result = val;
        setTimeout(()=>{
            this.resolveFn(val);
        },0);
        // 执行then的回调
        // this.resolveFn(val);
    };
    _reject(val){
        this.state = "rejected";
        this.result = val;
        setTimeout(()=>{
            this.rejectFn(val);
        },0);
        // 执行then的回调
        // this.rejectFn(val);
    };
    then(onResolved,onRejected){
        // console.log(onResolved,onRejected,'-----');
        //判断里面执行then里的参数,只能处理同步问题
        // if(this.state === "fulfilled"){
        //     onResolved && onResolved(this.result)
        // }else if(this.state === "rejected"){
        //     onRejected && onRejected(this.result)
        // };

        // 异步onResolved,onRejected不会立即执行;是在调取_resolve、_reject再执行
        // 保存onResolved、onRejected
        this.resolveFn = onResolved;
        this.rejectFn = onRejected;
    };
}

相关文章

  • Promise-02 then参数

    then 2个参数 : 1.异步 2.同步

  • 【promise-02】Promise较之回调的优势

    Promise 优势 使用promise去做一个流程的控制,肯定比回调要舒服很多,不管从维护上来讲还是可读性来讲,...

  • 参数,非参数,半参数

    非参数与参数理解 non-parametric model中参数是distribution free的,参数函数空...

  • 参数、非参数、半参数

    如何理解参数、非参数和半参数的概念?先回顾一下医学统计学参数检验和非参数检验的内容:参数检验(parametric...

  • 函数的参数

    函数的参数顺序是 必选参数、默认参数、可变参数、关键字参数、命名关键字参数 必选参数 默认参数 可变参数 关键...

  • 函数 参数

    关键字参数: 形如 kwarg = value 参数类型: 必备参数 命名参数 缺省参数 不定长参数 必备参数 ...

  • 3.monkey参数

    参数分类 常规类参数 事件类参数 约束类参数 调试类参数 常规类参数 常规类参数包括帮助参数和日志信息参数。帮助参...

  • Python之函数的参数

    Python中函数的参数有五种:位置参数(必选参数)、默认参数、可变参数、关键字参数、命名关键字参数。 位置参数 ...

  • 方法的参数 Day0815

    值参数 引用参数 输入参数 数组参数

  • 5.函数的参数

    位置参数 默认参数 可变参数 关键字参数 命名关键字参数 参数组合

网友评论

      本文标题:Promise-02 then参数

      本文链接:https://www.haomeiwen.com/subject/nxspektx.html