Last active
October 14, 2021 08:52
-
-
Save Andrew-He/a9cab55f4a2787ce3e7a4bab46b07df2 to your computer and use it in GitHub Desktop.
async pair adder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
simple async summer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function remoteExecutor(a, b) { | |
return Promise.resolve(a + b) | |
} | |
async function sum(arr) { | |
while (arr.length !== 1) { | |
let result = [] | |
for(let i = 0; i < arr.length - 1; i += 2) { | |
result.push(remoteExecutor(arr[i], arr[i + 1])) | |
} | |
if (arr.length % 2) { | |
result.push(arr[arr.length - 1]) | |
} | |
arr = await Promise.all(result) | |
} | |
return arr[0] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment