Javascript 如何将API响应时间从一个获取函数传递到另一个异步函数?
我能够在“makeAPICall”函数中获得API响应时间(持续时间)。 现在我需要将它(duration变量的值)传递给另一个异步函数。 我想知道你能否提供解决方案Javascript 如何将API响应时间从一个获取函数传递到另一个异步函数?,javascript,typescript,api,fetch-api,testcafe,Javascript,Typescript,Api,Fetch Api,Testcafe,我能够在“makeAPICall”函数中获得API响应时间(持续时间)。 现在我需要将它(duration变量的值)传递给另一个异步函数。 我想知道你能否提供解决方案 const makeApiCall = ClientFunction(() => { console.time("timer1"); const testLocation = () => fetch('https://xxxxxx', {method : 'GET', h
const makeApiCall = ClientFunction(() => {
console.time("timer1");
const testLocation = () => fetch('https://xxxxxx',
{method : 'GET',
headers:{
hash: 'xxxxx',
id: 'xxxxxxxxc'
}
})
.then(response => response.json())
.then(data => {
let duration = console.timeEnd("timer1");
console.log(duration);
});
return testLocation();
});
test('test', async t => {
await makeApiCall();
console.log(duration)?????
});
第一个问题:
console.timeEnd
不会返回任何内容,它会将所花的时间打印到控制台。改为使用performance.now()
,或者只使用Date
2) 然后,您应该返回上一次之后的持续时间
const makeApiCall = ClientFunction(() => {
const start = new Date().getTime();
const testLocation = () => fetch('https://xxxxxx',
{method : 'GET',
headers:{
hash: 'xxxxx',
id: 'xxxxxxxxc'
}
})
.then(response => response.json())
.then(data => {
const end = new Date().getTime();
return end - start;
});
return testLocation(); // This returns a Promise<number>
});
test('test', async t => {
const duration = await makeApiCall();
console.log(duration)?????
});
const makeApiCall=ClientFunction(()=>{
const start=new Date().getTime();
const testLocation=()=>fetch('https://xxxxxx',
{method:'GET',
标题:{
散列:“xxxxx”,
id:'XXXXXXXX C'
}
})
.then(response=>response.json())
。然后(数据=>{
const end=new Date().getTime();
返回结束-开始;
});
return testLocation();//这将返回一个承诺
});
测试('test',异步t=>{
const duration=await makeApiCall();
控制台日志(持续时间)?????
});
第一个问题:控制台。timeEnd
不会返回任何内容,它会将等待的时间打印到控制台。改为使用performance.now()
,或者只使用Date
2) 然后,您应该返回上一次之后的持续时间
const makeApiCall = ClientFunction(() => {
const start = new Date().getTime();
const testLocation = () => fetch('https://xxxxxx',
{method : 'GET',
headers:{
hash: 'xxxxx',
id: 'xxxxxxxxc'
}
})
.then(response => response.json())
.then(data => {
const end = new Date().getTime();
return end - start;
});
return testLocation(); // This returns a Promise<number>
});
test('test', async t => {
const duration = await makeApiCall();
console.log(duration)?????
});
const makeApiCall=ClientFunction(()=>{
const start=new Date().getTime();
const testLocation=()=>fetch('https://xxxxxx',
{method:'GET',
标题:{
散列:“xxxxx”,
id:'XXXXXXXX C'
}
})
.then(response=>response.json())
。然后(数据=>{
const end=new Date().getTime();
返回结束-开始;
});
return testLocation();//这将返回一个承诺
});
测试('test',异步t=>{
const duration=await makeApiCall();
控制台日志(持续时间)?????
});
@Hansmad感谢您的回复。不过我犯了另一个错误。我已经更新了我上面的第一篇文章error@Neginconsole.timeEnd
不返回时间,它将时间打印到console。“我改变了我的回答。”汉斯马谢谢你的回答。不过我犯了另一个错误。我已经更新了我上面的第一篇文章error@Neginconsole.timeEnd
不返回时间,它将时间打印到console。我改变了答案。