取一個我有以下陣營代碼:反應ES6一個
fetchA() {
fetch(aURL)
.then(response => response.json())
.then(json => {
this.setState({ a: json})
})
}
fetchB() {
fetch(bURL)
.then(response => response.json())
.then(json => {
this.setState({ b: json})
})
}
在網上大多數教程教你如如何wait until responses of both A and B been collected
:
Promise.all([fetchA, fetchB])
.then(values => {
console.log(values[0])
console.log(values[1])
})
但我要的是fetch A, when A finished<results been colleted> then fetch B
。
我該怎麼做?
謝謝你的時間!
======編輯=======
在我真正的代碼,我嘗試:
fetchA(){
fetch(aURL)
.then(fetchB())
.then(response => console.log(response))
.then(() => console.log('hi'))
}
fetchB(){
fetch(bURL)
.then((response) => console.log(response))
}
// call it
this.fetchA()
// output:
//
// A data
// hi
// B data
// what I want is:
//
// B data
// A data
// hi
* 「但我要的是獲取A,當A完成<結果被colleted>然後取B中。」 *如果你跳過了'when'在該聲明中,您基本上具有您需要編寫的代碼的文本版本。方法和順序甚至非常接近。 **只是不要忘記從fetchA和fetchB返回。** –