Shallow to shallow implementation of an asynchronous summation function

Simplification: sum of two numbers

Let's simply implement an asynchronous sum of two function

function sumT(a, b) {
    return await new Promise((resolve, reject) => {
        asyncAdd(a, b, (err, res) => {
            if(!err) {
                resolve(res)
            }
            reject(err)
        })
    })
}

// test
const test = await sumT(1, 2)
console.log(test)
// 3

Deepening: sum of the majority

Above, we realize the sum of two numbers, and then extend it to the sum of the majority?

When it comes to array summation, we first think of reduce

The reduce() method executes a reducer function (executed in ascending order) provided by you for each element in the array, and summarizes its results into a single return value.

- MDN

arr.reduce(callback(acc, cur[, idx[, arr]])[, initialValue])

The callback function receives four parameters:

  • acc: accumulator
  • cur: current value
  • idx: current index
  • arr: source array

initialValue is optional,

  • If initialValue: acc is initialValue, cur takes the first value in the array
  • If not: acc takes the first value in the array and cur takes the second value in the array
const arr = [1, 2, 3, 4];
const reducer = (acc, cur) => acc + cur;

// 1 + 2 + 3 + 4
console.log(arr.reduce(reducer));
// Output: 10

// 5 + 1 + 2 + 3 + 4
console.log(arr.reduce(reducer, 5));
// Output: 15

About this question: from @ champkeh

Set the initial value to promise Resolve (0), 5 times of summation:

function sum(...args) {
    return new Promise(resolve => {
        args.reduce((acc, cur) => acc.then(total => sumT(total, cur)), Promise.resolve(0)).then(resolve)
    })
}

// test
await sum(1, 2, 3, 4, 5)
// 15

However, it takes a long time. We can calculate the following time:

console.time("sum")
// test
await sum(1, 2, 3, 4, 5)
// 15
console.timeEnd("sum")

In other words, we spend 1s every time we sum, serial asynchronous sum, which is obviously not optimal

Optimization: using promise all

We can work in pairs and use promise All sum, then set the sum in pairs and continue to sum, Know that only one remaining is the final result

async function sum(...args) {
    // Used to examine the process of each iteration
    console.log(args) 

    // If there is only one, return directly
    if(args.length === 1) return args[0]
    let result = []
    // In pairs, if there is one left, enter directly
    for(let i = 0; i < args.length - 1; i+=2) {
        result.push(sumT(args[i], args[i + 1]))
    }
    if(args.length%2)  result.push(args[args.length-1])
    // Promise.all intra group summation
    return sum(...await Promise.all(result))
}

// test
test = await sum(1, 2, 3, 4, 5)
// 15

console.time("sum")
await sum(1, 2, 3, 4, 5)
console.timeEnd("sum")

last

This article starts from the "three minute learning front-end". Reply to "communication" and automatically join the front-end three minute advanced group. One programming algorithm interview question (including solutions) every day will help you become a better front-end developer! Reply "network" within the number, Automatically obtain the reply "JS" in the number of three minute pre-school front-end network small book (90 + pages), automatically obtain the reply "algorithm" in the number of three minute pre-school front-end JS small book (120 + pages), automatically obtain the front-end algorithm of github 2.9k +, reply "interview" in the small book number, and automatically obtain the reply "resume" in the front-end interview small book number of github 23.2k + , automatically obtain 120 sets of templates of the programmer series, and recently open source a github warehouse: ask questions and answer questions. It is difficult to answer community questions immediately at work, so you can submit the questions to https://github.com/Advanced-Frontend/Just-Now-QA , I will spend about 1 hour every night to deal with it, which is more to encourage and welcome more people to participate in discussion and answer 🌹

Added by phorcon3 on Mon, 03 Jan 2022 21:12:12 +0200