Determine Which Promise Is Slowest In Promises.all
I have been using Promise.all in my app. For the purpose of improving app speed, how to determine which promise is the slowest? const result = await Promise.all([ this.
Solution 1:
You can use a helper function for that:
asyncfunctiontime(p, name) {
const start = Date.now();
try {
returnawait p;
} finally {
const end = Date.now();
console.log(`${name} took ${end-start}ms`);
}
}
Then write
const result = await Promise.all([
time(this.props.fetchUser(), "user"),
time(this.props.cacheResourcesAsync(), "cacheResources"),
time(this.props.initAmplitude(), "amplitude"),
time(this.props.initAppVariables(), "appVars"),
]);
Solution 2:
I'd do something like this :
let startTime = newDate();
Promise.all([
this.fetchUser().then(() => {
console.log('fetch user takes', newDate().getTime()-startTime.getTime());
returnarguments;}),
this.fetchData().then(() => {
console.log('fetchData takes', newDate().getTime()-startTime.getTime());
returnarguments;})
]);
Post a Comment for "Determine Which Promise Is Slowest In Promises.all"