Leader, I feel this bug is targeting me

  1. This should be the most interesting bug I have encountered. Since it is a company intranet project, I will not show the screenshots of the project. This bug was asked by a friend of mine. Their company made a system for counting medical devices.

  1. Brief description of the function: Each row in the table can be selected and can be selected multiple times. Every time a row is selected, the total monthly sales of the equipment you selected in these rows will be counted at the bottom. The table has a large amount of data, and the monthly sales volume is not displayed in the table of each row. Instead, you need to get the id of this row when you click on this row, request the interface and get the monthly sales volume of this row, and then send your The monthly sales of the selected rows are added together, and finally the total monthly sales are displayed at the bottom.

  1. Strange point: this system was completed a year ago, during which time it was only used by his senior leaders (it has been ok), but after changing to a young leader recently, there is a problem when using this system to calculate monthly sales .

  1. Problem code:

// 极度简化的代码
async function fetchCount(id){//根据选中这行的id去请求获取这行的月销量
   传入id是多少返回月销量是多少
}

let count = 0 //统计设备的月销量

async function addCount(id){
    count += await fetchCount(id) //count = count + await fetchCount(id)
}
addCount(1) // count:0 + 等待  -> 0+1 = 1
addCount(2) // count:0 + 等待  -> 0+2 = 2
// 2

Multi-thread condition competition problem, js is not multi-threaded, but js is asynchronous.

Problems can arise if you mix asynchronous data with synchronous data in one expression—but the asynchronous behavior is concurrent.

Example: [...xxx , await xxx] will have problems

  1. easy to solve

// 极度简化的代码
async function fetchCount(id){//根据选中这行的id去请求获取这行的月销量
   传入id是多少返回月销量是多少
}

let count = 0 //统计设备的月销量

async function addCount(id){
    const n = await fetchCount(id)
    count = const + n 
}
addCount(1) 
addCount(2) 

Why doesn't the big leader have this bug? I lost it! Because the big leader is a bit too slow when he is older.

Guess you like

Origin blog.csdn.net/lovecoding1/article/details/129404087