sebba23
sebba23

Reputation: 574

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

Upvotes: 0

Views: 49

Answers (1)

Guillem Bonet
Guillem Bonet

Reputation: 56

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

Upvotes: 1

Related Questions