Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove awaits in loop #3979

Merged
merged 1 commit into from
Feb 15, 2020

Conversation

gittysachin
Copy link
Contributor

Fixes #3916

Short description of what this resolves:

If we're performing an operation on each element of an iterable and await is a part of each operation then the program is not taking full advantage of parallelization benefits of async/await. Each successive operation will not start until the previous one has completed.

Using Promise.all() to await all the requests that were kicked during the loop to finish will create all the promises at once.

This pull request fixes this issue.

Checklist

  • I have read the Contribution & Best practices Guide.
  • My branch is up-to-date with the Upstream development branch.
  • The acceptance, integration, unit tests and linter pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@auto-label auto-label bot added the fix label Feb 2, 2020
@codecov
Copy link

codecov bot commented Feb 2, 2020

Codecov Report

Merging #3979 into development will increase coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@               Coverage Diff               @@
##           development    #3979      +/-   ##
===============================================
+ Coverage        21.93%   21.93%   +<.01%     
===============================================
  Files              460      460              
  Lines             4733     4728       -5     
===============================================
- Hits              1038     1037       -1     
+ Misses            3695     3691       -4
Impacted Files Coverage Δ
app/controllers/public/index.js 0% <0%> (ø) ⬆️
app/controllers/events/view/edit/attendee.js 0% <0%> (ø) ⬆️
app/controllers/events/view/tickets/order-form.js 0% <0%> (ø) ⬆️
app/controllers/orders/new.js 0% <0%> (ø) ⬆️
app/mixins/event-wizard.js 1.26% <0%> (-0.04%) ⬇️
app/controllers/events/view/tickets/add-order.js 0% <0%> (ø) ⬆️
app/models/event.js 53.84% <0%> (-7.7%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a8aac00...8b0ca3a. Read the comment docs.

}
}));
propsToSave.map((property, index) => {
promises.then(promise => { data[property] = promise[index] });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are already resolved promises. No need to then these promises

} catch (e) {
if (!(e.errors && e.errors.length && e.errors.length > 0 && e.errors[0].status === 404)) {
// Lets just ignore any 404s that might occur. And throw the rest for the caller fn to catch
throw e;
}
}
}
}));
propsToSave.map((property, index) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use for of loop

@gittysachin
Copy link
Contributor Author

Sorry for the inconvenience, I'm having problems while using rebase. I'm learning. I'll not make these mistakes in the future.

@gittysachin
Copy link
Contributor Author

@iamareebjamal I did everything right this time besides the force push. Then why it is showing that the codecov/patch was not successful?

} catch (e) {
if (!(e.errors && e.errors.length && e.errors.length > 0 && e.errors[0].status === 404)) {
// Lets just ignore any 404s that might occur. And throw the rest for the caller fn to catch
throw e;
}
}
}));
for (const [index, property] of propsToSave.entries()) {
data[property] = promises[index];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to assign only if it was successful

@@ -48,28 +48,31 @@ export default Mixin.create(MutableArray, CustomFormMixin, {
async saveEventData(propsToSave = []) {
const event = this.get('model.event');
const data = {};
for (const property of propsToSave) {
const promises = await Promise.all(propsToSave.map(property => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Promise.allSettled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last commit didn't pass. I replaced Promise.all with Promise.allSettled. What else should I do?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See what allSettled returns

}
}));
results.then(promises => {
for (const [index, property] of propsToSave.entries()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to extract index

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then how can I assign the value of event.get(property) without using the index?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using property

@niranjan94
Copy link
Member

Codacy Here is an overview of what got changed by this pull request:

Issues
======
+ Solved 10
           

See the complete overview on Codacy

@iamareebjamal iamareebjamal changed the title fix: Remove awaits in loop (Revised) fix: Remove awaits in loop Feb 15, 2020
@iamareebjamal iamareebjamal merged commit 0456445 into fossasia:development Feb 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove awaits in loop
3 participants