简体   繁体   中英

cannot get to resolve promises in javascript

I'm playing with promises but when I do some nesting I get stuck. For example in the code below, I get in the console 'machine type a = 1' and 'machine type c = something'. Why It doesn't resolve machineTypeC? Is it because it's too fast? I don't understand.

Thank you for any help

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')
          }
        })
      })
    })
   }

You are not returning any value, each path in the function should lead to a return statement. For example:

.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
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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