0

I would like to calcul and display in a file the elapsed time for each call response.

Promise function

function callPromise(ms, promise) {
    return new Promise((resolve, reject) => {
        const timeoutId = setTimeout(() => {
            reject(new Error(`Timeout after ${ms} ms`))
        }, ms);
        promise.then(
            (res) => {
                clearTimeout(timeoutId);
                resolve(res);
            },
            (err) => {
                clearTimeout(timeoutId);
                reject(err);
            }
        );
    })
}

Response call

        const response = await callPromise(10, fetch(url, {
            method: request.method,
            headers: header,
            body: body
        }))
0

1 Answer 1

2

With console.time() ?

console.time("Elapsed time :");
const response = await callPromise(...);
console.timeEnd("Elapsed time :"); // Will log "Elapsed time : 6.9165ms"
Sign up to request clarification or add additional context in comments.

3 Comments

yes but I dont need that on the console but with logger.info !
And why didn't you say so in your question :/ You said "I would like to calcul and display" and that's what my answer does.
sorry my bad :( ! Post edited

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.