繁体   English   中英

无法解决 javascript 中的承诺

[英]cannot get to resolve promises in javascript

我在玩承诺,但是当我做一些嵌套时,我被卡住了。 例如,在下面的代码中,我在控制台中输入“machine type a = 1”和“machine type c = something”。 为什么它不解析machineTypeC? 是不是因为太快了? 我不明白。

感谢您的任何帮助

refreshData() {
    this.getRegistrationNumber().then(() => {
      return this.swVersionRequest()
    })

    .then(() => { 

      this.getMachineTypeA().then(machineTypeA => {
        if (machineTypeA[0] === 1) {
          console.log('machine type a = 1')
        } else if (machineTypeA[0] === 2) {
          console.log('machine type a = 2')
        } else {
          console.log('machine type a = something')
        }
      })
      .then(() => {

        this.getMachineTypeB().then(machineTypeB => {
          if (machineTypeB[0] === 1) {
            console.log('machine type b = 1')
          } else if (machineTypeB[0] === 2) {
            console.log('machine type b = 2')
          } else {
            console.log('machine type b = something')
          }
        })
      })
      .then(() => {

        this.getMachineTypeC().then(machineTypeC => {
          if (machineTypeC[0] === 1) {
            console.log('machine type c = 1')
          } else if (machineTypeC[0] === 2) {
            console.log('machine type c = 2')
          } else {
            console.log('machine type c = something')
          }
        })
      })
    })
   }

您没有返回任何值,函数中的每个路径都应指向 return 语句。 例如:

.then(() => { 

  return this.getMachineTypeA().then(machineTypeA => {
    if (machineTypeA[0] === 1) {
      console.log('machine type a = 1')
    } else if (machineTypeA[0] === 2) {
      console.log('machine type a = 2')
    } else {
      console.log('machine type a = something')
    }
    return
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM