[英]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.