Hey all。チェーンフェッチの約束(JSON、React、Webpack、NodeJS)
私は何か10回取り出して、各要求の.JSONを返し、それをStateにプッシュする必要があります。 ほとんどのフェッチは機能しますが、完了する前に半分ほど遅くなり、停止します。 約束は、実際のjsonではなく「cors」応答を受け入れているように見え、早期にトリガーします。
また、オフセットを変更するために同じコードを何度も繰り返す必要があるのが好きではありませんが、forループを使って時間を費やしてしまいました。これを行う方法はawesomeです。
は、ここでは、コードです:あなたはwoneringた場合
function FetchAll(API_KEY, CX, query, E, that, pushState){
fetch(`https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=${E.target.value}&key=${API_KEY}`, {
method: 'get',
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).then(function(response){
return response.json()
}).then(function(uploads){
console.log(uploads)
return fetch(`https://www.googleapis.com/youtube/v3/playlistItems?playlistId=${uploads.items[0].contentDetails.relatedPlaylists.uploads}&key=${API_KEY}&part=snippet&maxResults=50`)
}).then(response => {
return response.json()
}).then(function(data){
console.log(data)
that.setState({yt:data})
}).then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${1}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${11}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${21}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${31}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${41}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${51}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${61}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${71}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${81}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${91}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
.then(function(){
return fetch(`https://www.googleapis.com/customsearch/v1?key=${API_KEY}&num=10&cx=${CX}&q=${query}&start=${101}`)
}).then(function(response){
return response.json();
}).then(r => pushState(r))
}
export default FetchAll
、fetchAllのパラメータでそれに送られるすべてのもので、メインApp.jsファイルに呼び出されています。
これは 'この'
任意のヘルプまたはチップは非常に高く評価されている。への参照である 'こと' pushState(必要な場合)
FetchAll(API_KEY, CX, query, e, that,
(r) => {
console.log(r)
let c = that.state.compareRaw
c.push(r)
that.setState({
compareRaw:c
})
}
)
}
ありますありがとう!
reactyをタグ付けしているのでasync/awaitを使用してみてください。あなたがreduxを使用している場合は、副作用を処理するために「redux-saga」を使用することをおすすめしますか? –
あなたは観察している症状をより正確にすることができますか? "フェッチ"のどの部分がうまくいかないのですか?あなたが働いているインスタンスとそうでないインスタンスの間では、どのような違いがありますか? 「半分ほど遅くなる」とはどういう意味ですか?半分はどこですか?完了する前にどのように停止していますか?エラーがスローされますか?要求は応答を受信していませんか?特定のデータが実際に何か他のときにjsonになると間違って予測しているとわかった場合は、現在その問題を解決できない原因は何ですか? – fvgs
上記のように、ここでは、依存関係を記述するためのasync/awaitは良好です。しかし、お互いに依存しないリクエストや作業では、配列(map/forEach/loopと一緒に)と 'Promise.all()はコードをもっと良くするでしょう。 – fvgs