Javascript 如何通过.then()链保持响应url和响应正文?

Javascript 如何通过.then()链保持响应url和响应正文?,javascript,promise,fetch,Javascript,Promise,Fetch,我试图保留我的响应url和响应正文,以便进一步处理。 如果像平常一样返回res.json(),则会丢失res.url值 但是如果我添加代码来获取res.url,那么我就很难解析res.json()承诺 我上一次尝试是尝试为res.json()创建一个异步/等待函数,但这对我也不起作用: fetch(url, options).then((res)=>{ let obj = getJSON(res); let profileUrn = res.url.match(/(?&l

我试图保留我的响应url和响应正文,以便进一步处理。 如果像平常一样返回res.json(),则会丢失res.url值

但是如果我添加代码来获取res.url,那么我就很难解析res.json()承诺

我上一次尝试是尝试为res.json()创建一个异步/等待函数,但这对我也不起作用:

 fetch(url, options).then((res)=>{
    let obj = getJSON(res);
    let profileUrn = res.url.match(/(?<=urn%3Ali%3Afsd_profile%3A).+(?=&)/)[0];
    let rep = {
        profileUrn,
        obj
    };
    return rep;
}
).then((data)=>{
    console.log(data.profileUrn);
    console.log(data.obj);
}
);

async function getJSON(res){
        let data = await res.json();
        return data;    
    };
fetch(url,选项)。然后((res)=>{
设obj=getJSON(res);
让profileUrn=res.url.match(/(?){
console.log(data.profileUrn);
console.log(data.obj);
}
);
异步函数getJSON(res){
让data=wait res.json();
返回数据;
};

尝试将第一个
then()
回调
async
然后您可以等待
res.json()

fetch(url,选项)。然后(异步(res)=>{
//^^^异步
设obj=wait res.json();
//^^等待json承诺
让profileUrn=res.url.match(/(?){
console.log(data.profileUrn);
console.log(data.obj);
});

注意:您需要添加额外的错误处理功能

尝试只进行第一次
then()
回调
async
,然后您可以等待
res.json()

fetch(url,选项)。然后(异步(res)=>{
//^^^异步
设obj=wait res.json();
//^^等待json承诺
让profileUrn=res.url.match(/(?){
console.log(data.profileUrn);
console.log(data.obj);
});

注意:您需要自己添加额外的错误处理

如果您想在不使用
async/await
关键字的情况下实现这一点,可以使用
Promise。all
返回承诺和非承诺的数组:

fetch(url, options)
  .then(res => Promise.all([res.url, res.json()]))
  .then(([url, obj]) => {
    console.log(url);
    console.log(obj);
  });
当iterable包含已实现的承诺和已返回的非承诺时,all()方法返回一个已实现的承诺

或者,当
res
仍在同一范围内时,将
json()
承诺链接起来:

fetch(url, options)
  .then(res => res.json().then(obj => [res.url, obj])) 
  .then(([url, obj]) => {
    console.log(url);
    console.log(obj);
  });

如果您想通过
async/await
关键字实现这一点:

// These must be used inside an async function.

const res = await fetch(url, options)
const obj = await res.json();
const url = res.url;

可能有更多的方法可以做到这一点(包括@charlietfl的答案)。试着理解它们并选择一种适合你的风格。JavaScript很有趣!

如果你想在没有
async/wait
关键字的情况下实现这一点,你可以使用
Promise。all
返回承诺和非承诺的数组:

fetch(url, options)
  .then(res => Promise.all([res.url, res.json()]))
  .then(([url, obj]) => {
    console.log(url);
    console.log(obj);
  });
当iterable包含已实现的承诺和已返回的非承诺时,all()方法返回一个已实现的承诺

或者,当
res
仍在同一范围内时,将
json()
承诺链接起来:

fetch(url, options)
  .then(res => res.json().then(obj => [res.url, obj])) 
  .then(([url, obj]) => {
    console.log(url);
    console.log(obj);
  });

如果您想通过
async/await
关键字实现这一点:

// These must be used inside an async function.

const res = await fetch(url, options)
const obj = await res.json();
const url = res.url;

可能有更多的方法可以做到这一点(包括@charlietfl的答案)。试着理解它们并选择一种适合你的风格。JavaScript很有趣!

巧妙地使用Promise。all()谢谢,这个问题让我读了
Promise.all的MDN文档。通过教学学习很好。巧妙地使用Promise.all()谢谢,这个问题让我读了《承诺》的MDN文档。所有的
。通过教学学习是很好的。以下是您的各种选项列表:。以下是您的各种选项列表:。