Skip to content

Instantly share code, notes, and snippets.

@Andrew-He
Last active October 14, 2021 08:52
Show Gist options
  • Save Andrew-He/a9cab55f4a2787ce3e7a4bab46b07df2 to your computer and use it in GitHub Desktop.
Save Andrew-He/a9cab55f4a2787ce3e7a4bab46b07df2 to your computer and use it in GitHub Desktop.
async pair adder
simple async summer
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