[转载]Javascript .then()这个方法是什么意思?

  1. then()方法是异步执行。

  2. 意思是:就是当.then()前的方法执行完后再执行then()内部的程序,这样就避免了,数据没获取到等的问题。

  3. 语法:promise.then(onCompleted, onRejected);

  4. 参数

  • promise必需。Promise 对象。

  • onCompleted必需。承诺成功完成时要运行的履行处理程序函数。

  • onRejected可选。承诺被拒绝时要运行的错误处理程序函数。

代码举例:

start() {
        super.start();
        if (this.transport) {
            return this.transport.ready.then(() => {
                return this.clientSM.start();
            });
        } else {
            return Promise.reject("no transport attached");
        }
    }
原文地址:https://www.cnblogs.com/swordenchanter/p/9112107.html