Skip to content

refactor(rakmaty-survey): never use await in a loop, add editorconfig#1

Open
marekvospel wants to merge 3 commits intoklubFITpp:mainfrom
marekvospel:optimize-survey
Open

refactor(rakmaty-survey): never use await in a loop, add editorconfig#1
marekvospel wants to merge 3 commits intoklubFITpp:mainfrom
marekvospel:optimize-survey

Conversation

@marekvospel
Copy link
Member

When using await inside a loop, each iteration has to wait till the previous one is finished - each request is blocking the whole computation. I refactored the code to be less nested and to use the Promise utilities and Array prototype functions

Promise.all([promise1, promise2]) - starts all promises at the same time, waits till all are done or till one rejects (error is thrown)
Promise.allSettled[promise1, promise2]) - starts all promises at the same time, waits till all are done, doesnt fail if promise rejects

[1, 2, 3, 4].filter(num => num > 2) filters elements based on supplied predicate (returns new array with elements that satisfy said predicate)
[true, false, undefined, null, true].filter(Boolean) filters based on whether value is truthy / falsy - boolean constructor is used as a predicate
[[1, 2], [3, 4]].flat() flattens nested arrays into a single array (isnt recursive) - returns [1, 2, 3, 4]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant