45 Star 1.2K Fork 80

Lee / uv-ui

 / 详情

节流函数对异步操作失效

已完成
创建于  
2023-11-24 11:29

节流函数对范例中的同步操作有效,但是对于async await这种访问数据库的异步操作无效,无法达到节流的效果,请您更正。

评论 (6)

blazeware 创建了任务

你可以提供一段示例哦

`<script setup>
const change=async()=>{
uni.$uv.throttle(await dataGet(),1000)
}
const dataGet=async()=>{
let res = await uniCloud.database().collection('circle-articles').get({getOne:true})
console.log(res);
return res
}

</script>`

上述代码中'circle-articles'是我自建的集合名称,您替换成您自建的数据库名称,就会发现,这个异步的数据库读取操作是无法节流的,连续快速点击会连续返回数据,应该是promise对象的pending状态在您的节流函数中没有当做函数已经触发。

我用的是unicloud的云服务空间哈,但是问题应该不在服务空间上,我看了一下您的源码,应该是没有对promise对象的pending状态进行控制,所以pending没有返回值的时候节流函数就没起作用。

<script setup>
	const change=async()=>{
		uni.$uv.throttle(await dataGet(),1000)
	}
	const dataGet=async()=>{
		let res = await uniCloud.database().collection('circle-articles').get({getOne:true})
		console.log(res);
		return res
	}


</script>
<script setup>
	const change=async()=>{
		uni.$uv.throttle(dataGet,1000)
	}
	const dataGet=async()=>{
		let res = await uniCloud.database().collection('circle-articles').get({getOne:true})
		console.log(res);
		return res
	}


</script>

感谢大佬,原来是我异步函数使用方法有误,这会就行了,非常感谢

Lee 任务状态待办的 修改为进行中
Lee 任务状态进行中 修改为已完成

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
1
https://gitee.com/climblee/uv-ui.git
git@gitee.com:climblee/uv-ui.git
climblee
uv-ui
uv-ui

搜索帮助

344bd9b3 5694891 D2dac590 5694891