回调 函数解决异步,promise解决异步

 created() {

    // ===============================================回调函数解决异步

    // this.getList((res) => {

    //   this.$message.success("获取数据成功");

    //   const firstData = res.data;

    //   // 再次获取数据,需要依赖于第一次获取的数据

    //   this.getList2((finalData) => {

    //     console.log(finalData);

    //     this.tableData = finalData;

    //   }, firstData);

    // });

    // // ===============================================promise解决异步

    // getList的执行结果就是Promise构造出来的实例,实例有then和catch方法

    // this.getList()

    //   .then((res1) => {

    //     // getList2就必须依赖res1才可以执行

    //     this.getList2(res1.data).then((res2) => {

    //       this.tableData = res2;

    //     });

    //   })

    //   .catch((err) => {

    //     alert(err);

    //   })

    //   .finally(() => {

    //     alert("我都会执行");

    //   });

    // this.getList()

    //   // 当在then函数中,返回新的promise实例的时候,可以继续.then链式调用,并且只要有任何一个then函数报错,都会被最后一个catch捕获

    //   .then((res1) => {

    //     // getList2就必须依赖res1才可以执行

    //     return this.getList2(res1.data);

    //   })

    //   .then((res2) => {

    //     this.tableData = res2;

    //     return res2; // 当返回一个常量必然走下一个then,必然成功

    //   })

    //   .then((res3) => {

    //     // res3 === res2;

    //   })

    //   .catch((err) => {

    //     alert(err);

    //   })

    //   .finally(() => {

    //     alert("我都会执行");

    //   });

    // this.getList3()

    //   .then((res) => {

    //     alert(JSON.stringify(res));

    //   })

    //   .catch((err) => {

    //     // 不走

    //   });

    // this.getList4()

    //   .then((res) => {

    //     // 不走

    //   })

    //   .catch((error) => {

    //     alert(JSON.stringify(error));

    //   });

  },

 

// ===============================================回调  函数解决异步

    // getList(callback) {

    //   this.loading = true;

    //   setTimeout(() => {

    //     const res = {

    //       data: [

    //         {

    //           name: "oppo",

    //           todayBuy: 100,

    //           monthBuy: 300,

    //           totalBuy: 800,

    //         },

    //         {

    //           name: "vivo",

    //           todayBuy: 100,

    //           monthBuy: 300,

    //           totalBuy: 800,

    //         },

    //         {

    //           name: "苹果",

    //           todayBuy: 100,

    //           monthBuy: 300,

    //           totalBuy: 800,

    //         },

    //       ],

    //     };

    //     this.loading = false;

    //     callback(res);

    //   }, 2000);

    // },

    // getList2(callback, firstData) {

    //   setTimeout(() => {

    //     const res = {

    //       data: [

    //         {

    //           name: "小米",

    //           todayBuy: 100,

    //           monthBuy: 300,

    //           totalBuy: 800,

    //         },

    //         {

    //           name: "三星",

    //           todayBuy: 100,

    //           monthBuy: 300,

    //           totalBuy: 800,

    //         },

    //         {

    //           name: "魅族",

    //           todayBuy: 100,

    //           monthBuy: 300,

    //           totalBuy: 800,

    //         },

    //       ],

    //     };

    //     this.loading = false;

    //     callback([...firstData, ...res.data]);

    //   }, 2000);

    // },

    // // ===============================================promise解决异步

    // getList() {

    //   this.loading = true;

    //   return new Promise((resolve, reject) => {

    //     setTimeout(() => {

    //       const res = {

    //         data: [

    //           {

    //             name: "oppo",

    //             todayBuy: 100,

    //             monthBuy: 300,

    //             totalBuy: 800,

    //           },

    //           {

    //             name: "vivo",

    //             todayBuy: 100,

    //             monthBuy: 300,

    //             totalBuy: 800,

    //           },

    //           {

    //             name: "苹果",

    //             todayBuy: 100,

    //             monthBuy: 300,

    //             totalBuy: 800,

    //           },

    //         ],

    //       };

    //       this.loading = false;

    //       // promise的resolve和reject是两个函数,resolve就是成功,reject就是失败

    //       // promise状态一旦确定,就不会变了,所以实例的then和catch只会走一个

    //       resolve(res);

    //       // reject("获取数据失败");

    //     }, 1000);

    //   });

    // }, // getList的执行结果就是Promise构造出来的实例,实例有then和catch方法

    // getList2(firstData) {

    //   this.loading = true;

    //   return new Promise((resolve, reject) => {

    //     setTimeout(() => {

    //       const res = {

    //         data: [

    //           {

    //             name: "oppo",

    //             todayBuy: 100,

    //             monthBuy: 300,

    //             totalBuy: 800,

    //           },

    //           {

    //             name: "vivo",

    //             todayBuy: 100,

    //             monthBuy: 300,

    //             totalBuy: 800,

    //           },

    //           {

    //             name: "苹果",

    //             todayBuy: 100,

    //             monthBuy: 300,

    //             totalBuy: 800,

    //           },

    //         ],

    //       };

    //       this.loading = false;

    //       // promise的resolve和reject是两个函数,resolve就是成功,reject就是失败

    //       // promise状态一旦确定,就不会变了,所以实例的then和catch只会走一个

    //       resolve([...firstData, ...res.data]);

    //       // reject("获取数据失败");

    //     }, 1000);

    //   });

    // },

    // getList3() {

    //   // Promise 类的方法

    //   // resolve 快速创建一个成功的p实例

    //   return Promise.resolve([

    //     {

    //       name: "小米",

    //       todayBuy: 100,

    //       monthBuy: 300,

    //       totalBuy: 800,

    //     },

    //   ]);

    // },

    // getList4() {

    //   // Promise 类的方法

    //   // resolve 快速创建一个成功的p实例

    //   return Promise.reject("失败");

    // },

你可能感兴趣的:(java,前端,javascript)