我可以在'makeAPICall‘函数中获得接口响应时间(持续时间)。现在我需要将它( duration变量的值)传递给另一个异步函数。我想知道您是否可以提供解决方案?
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)?????
});发布于 2021-05-27 13:56:04
第一个问题:console.timeEnd没有返回任何东西,它将ellapsed时间打印到控制台。请改用performance.now()或仅使用Date。
2)然后返回上一次then的持续时间。
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)?????
});https://stackoverflow.com/questions/67715737
复制相似问题