then()函数用法

Posted on 2020-07-16 17:06  LuckyCola  阅读(6944)  评论(1)    收藏  举报

then()方法是异步执行

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

 

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

 

参数

promise必需。Promise 对象。

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

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

fetch("./json/test.json").then(res=>{
           return res.json()
       }).then(res=>{
           console.log(res)
       })