Reputation: 574
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
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