Asynchronous 如何返回递归获取的结果?

Asynchronous 如何返回递归获取的结果?,asynchronous,recursion,fetch,Asynchronous,Recursion,Fetch,我有第一个异步函数 fetch("https://api.priceapi.com/v2/jobs", { body: body, headers: { "Content-Type": "application/x-www-form-urlencoded" }, method: "POST" }).then((response) =>

我有第一个异步函数

fetch("https://api.priceapi.com/v2/jobs", {
            body: body,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: "POST"
                }).then((response) => {
                            return response.json();
                        }).then((data) => {

                            return fetchRepeat(data.job_id)
                        })

第二个递归异步函数

function fetchRepeat(id){

    fetch("https://api.priceapi.com/v2/jobs/"+ id +"/download.json?token=" + priceapisecret.secret)
    .then((response) => {
        return response.json()
    }).then((data) =>{
        if(data.status == "finished"){

            var bookdata = {
                title: data.results[0].content.name,
                price: data.results[0].content.price
            }

            return bookdata;

        }
        else{
            fetchRepeat(id)
        }
    })
}

我希望能够在第一个异步函数中访问bookdata。我该怎么做呢?

为了谈论退货,您的
回执需要退货。它没有这样做,结果是返回
未定义的
。最后一个
then
也没有返回递归的值,因此也被解析为
undefined

以下是一个工作版本:

函数fetchRepeat(id){
//还债
回传(`https://api.priceapi.com/v2/jobs/${id}/download.json?令牌=${priceapisecret.secret}`)
.then(response=>response.json())
。然后({状态,结果:[{content:{name:title,price}}]=[{content:{}]}]=>
(status==“finished”?{title,price}:fetchRepeat(id));//返回递归结果
}

现在我让ESLint处理格式,因为我使用airbnb,它更喜欢解构。最后一个
然后
中的错误是显而易见的,因为ELSint抱怨一致的返回。我建议您使用一个linter和一个IDE,它强制执行一种编码风格,以减少代码中的bug,并使其他人更容易阅读

根据OP的说法,在完成任务之前,您的解构需要稍微混乱一些:
。然后({status,results:[{{content:{name:title,price}={}]=[]})=>…
@user633183修复。谢谢:)