疯狂嵌套Promise异步,最终获取总结的方法,只要知道Promise是什么都能看懂的一块儿代码,贼**简单。

文章描述了一个使用JavaScript的reduce方法优化电影列表中每个电影评论总数获取和排序的过程,最后将数据用于生成评论数量图表。
摘要由CSDN通过智能技术生成

gmv() {
		this.get("/prod-api/api/movie/film/list").then(res => {
			return res.rows.map(async item => {
				return await this.get("/prod-api/api/movie/film/comment/list", {
					'movieId': item.id
				}).then(res => {
					return {
						n: item.name,
						c: res.total
					}
				})
			})
		}).then(resc => {
			Promise.all(resc).then(i => {
				i.sort((a,b)=>b.c-a.c)
				this.chd.cd.categories = i.slice(0,5).map(item=>item.n)
				this.chd.cd.series = [{
					type:"scatter",
					name:"评论数",
					data:i.slice(0,5).map(item=>item.c)
				}]
			})
		})
	}

reduce 版本。。

gmv() {
	this.get("/prod-api/api/movie/film/list").then(res => res.rows.reduce((acc, item) => {
		acc.push(this.get("/prod-api/api/movie/film/comment/list", {
			'movieId': item.id
		}).then(res => ({
			n: item.name,
			c: res.total
		})))
		return acc
	}, []))
	.then(resc => {
	Promise.all(resc).then(i => {
	i.sort((a, b) => b.c - a.c)
	this.chd.cd.categories = i.slice(0, 5).map(item => item.n)
	this.chd.cd.series = [{
				type: "scatter",
				name: "评论数",
				data: i.slice(0, 5).map(item => item.c)
			}]
		})
	})
},

大概就这么个逻辑,简单的一批,别再费脑子想了,赶紧度娘csdn😂😂😂😂😂🤣

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值