javascript 实时搜索节点节流

let timeout
export const asyncReadSelect = (fileLabelValue, callback, needLable = false) => {
  return new Promise((resolve) => {
    if (timeout) {
      clearTimeout(timeout)
      timeout = null
    }
    timeout = setTimeout(() => {
      callback().then((result) => {
        const data = []
        result.forEach((r) => {
          data.push({
            label: r[fileLabelValue[0]],
            // value: r[fileLabelValue[1]]
            value: needLable ? `${r[fileLabelValue[0]]},${r[fileLabelValue[1]]}` : r[fileLabelValue[1]]
          })
        })
        resolve(data)
      })
    }, 300)
  })
}

调用

 getProductNameEffects = async (value) => {
    const getProductNameData = await asyncReadSelect(['productName', 'productName'], () => {
      return Api.Search({ productName: value.replace(/[']/g, '') })
    })
    return getProductNameData
  }

 

你可能感兴趣的:(javascript)