簡體   English   中英

使用 github api 的嵌套獲取返回的是承諾而不是數據?

[英]nested fetching using github api is returning promises instead of data?

所以我試圖從存儲庫中存在的每個文件夾中獲取文件 description.txt。 這就是我想出的。

useEffect(() => {
const apiUrl = `https://api.github.com/repos/afhammk/components/contents`
fetch(apiUrl)
  
  .then((res) => res.json())
  .then((repos) => {
    console.log("reps",repos)
    
    const s=repos.map((comps)=>{
    let arr=[]
    return {
             name:comps.name , 
             html_url:comps.html_url,
             desc:fetch(comps.url)
                  .then((res)=>res.json())
                  .then((repos)=>repos[0].url)
                  .then((newurl)=>{
                    return(
                      fetch(newurl)
                      .then((res)=>res.json())
                      .then((data)=>{
                           return atob(data.content)
                    })
                    )
                  }
                  )
              
            }
    
    }
    
    )
   
    setAppState(s)
  });
  }, []);

    

但是在 output 中,“desc”值是一個 promise 實現了數據而不是僅數據。

desc: Promise {<fulfilled>: "button"}
html_url: "https://github.com/afhammk/components/tree/main/button"
name: "button"
__proto__: Object
1:
desc: Promise {<fulfilled>: "created:hisham↵description:kkfmkmdkmfkdmkfmdkmkfm↵"}
html_url: "https://github.com/afhammk/components/tree/main/textfield"
name: "textfield"
__proto__: Object
2:
desc: Promise {<fulfilled>: "tree↵"}
html_url: "https://github.com/afhammk/components/tree/main/tree"
name: "tree"

將嵌套提取包裝在 promise 中,然后返回此 promise

嘗試

 useEffect(() => { const apiUrl = `https://api.github.com/repos/afhammk/components/contents` fetch(apiUrl).then((res) => res.json()).then((repos) => { console.log("reps", repos) Promise.all(repos.map((comps) => { return fetch(comps.url).then((res) => res.json()).then((repos) => repos[0].url).then((newurl) => { return ( fetch(newurl).then((res) => res.json()).then((data) => { return atob(data.content) }) ) }).then((desc) => { return { name: comps.name, html_url: comps.html_url, desc, } }) })).then((s)=>{ setAppState(s) }) }); }, []);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM