4种 promise 写法有什么区别

2020-04-17 01:43:03 蜻蜓队长

doSomething().then(function () {
  return doSomethingElse();
});
把doSomethingElse方法的返回值作为下一个then函数的callback的参数

doSomething().then(function () {
  doSomethingElse();
});
没有实际意义,只是单纯的调用了doSomethingElse方法

doSomething().then(doSomethingElse());
doSomethingElse方法的返回值,将作为then方法的回调函数

doSomething().then(doSomethingElse);
doSomethingElse方法作为回调函数传入

.then()
1、接收两个函数作为参数,分别代表fulfilled(成功)和rejected(失败)
2、.then()返回一个新的Promise实例,所以它可以链式调用
3、当前面的Promise状态改变时,.then()根据其最终状态,选择特定的状态响应函数执行
4、状态响应函数可以返回新的promise,或其他值,不返回值也可以我们可以认为它返回了一个null;
5、如果返回新的promise,那么下一级.then()会在新的promise状态改变之后执行
6、如果返回其他任何值,则会立即执行下一级.then()

JavaScript code
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
          id: 1,
          date: "2016-05-02",
          name: "sdsd对数函数的 ",
          levels: 1,
          address: "上海市普陀区金沙江路 1518 弄",
          children: [
            {
              id: 11,
              date: "2016-05-01",
              name: "万二份",
              levels: 2,
              address: "上海市普陀区金沙江路 1519 弄",
              children: [
                {
                  id: 111,
                  date: "2016-05-01",
                  levels: 3,
                  name: "王三",
                  address: "上海市普陀区金沙江路 1519 弄"
                }
              ]
            }
          ]
        },



表单组件

JavaScript code
 
?
1
2
3
4
5
6
7
8
 <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      stripe
      ref="refs" 
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
XML/HTML code
 
?
1
 



我写的方法是这样的

JavaScript code
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
sortDelet(row) {
      //   console.log(this);
      console.log(row);
      console.log(row.levels);
 
      if (row.levels * 1 == 1) {
        this.tableData.splice(this.tableData.indexOf(row), 1);
      else if (row.levels * 1 == 2) {
        // debugger;
        let child1 = [];
        for (let i = 0; i <= this.tableData.length; i++) {
          child1 = this.tableData[i].children;
          for (let j = 0; j < child1.length; j++) {
            if (child1[j].id * 1 == row.id*1) {
              child1.splice(child1.indexOf(child1[j]), 1);
            }
          }
        }
      else if (row.levels * 1 == 3) {
        let child1 = [];
        let child2 = [];
        for (let i = 0; i <= this.tableData.length; i++) {
          
 
          child1 = this.tableData[i].children;
          for (let j = 0; j < child1.length; j++) {
            child2 = this.tableData[i].children[j].children;
            for (let k = 0; k < child2.length; k++) {
              if (child2[k].id * 1 == row.id*1) {
                child2.splice(child2.indexOf(child2[k]), 1);
              }
            }
          }
        }
      }
    }



报错是这样的
但是奇怪的是,功能实现了,但是一直报错,
Error in v-on handler: "TypeError: Cannot read property 'children' of undefined"

以上内容来自于网络,如有侵权联系即删除
相关文章

上一篇: PIL库使用小测试

下一篇: 628. Maximum Product of Three Numbers

客服紫薇:15852074331
在线咨询
客户经理