vue里promise的使用

在用vue做项目的时候,有一个业务场景是发布一个带有附件的消息,后台接口的设计分为两部分:先发布消息内容,返回该消息的id,然后以该消息id为参数,上传消息附件。所以在后一个接口发起请求的时候,前一个接口必须已经请求成功。

添加消息:

addMessage() {
	return new Promise((resolve, reject) => {
		addMessage({
			messageContent: this.messageContent
		}).then((data) => {
			resolve(data.id)
		})
		
	})
}

添加附件:

addAnnex() {
	return new Promise((resolve, reject) => {
			this.$refs.upload.submit();
			resolve()
	});
}

发布消息:

publish() {
	this.addMessage().then((res) => {
		console.log(res)
		return this.addAnnex()
	})
}

猜你喜欢

转载自blog.csdn.net/Lycoriy/article/details/108276957