多个Promise循环和 循环里面异步问题示例

javascript 复制代码
export function obsPost(data = []) {
	return new Promise(function(resolve, reject) {
		if (!data || data.tempFilePaths.length <= 0) {
			return reject('上传正确数据!')
		}
		let linshi_arr = [],
			true_arr = [],
			fileName = '',
			suffix = '',
			path = '',
			tempName = '';

		for (let i in data.tempFiles) {
			fileName = data.tempFiles[i].name //文件名字
			suffix = fileName.substring(fileName.lastIndexOf(".") + 1)
			tempName = data.tempFilePaths[i]
			tempName = tempName.substring(tempName.lastIndexOf("/") + 1)
			path = suffix + '/' + tempName + '-' + (Date.parse(new Date()) / 1000) + '.' + suffix
			linshi_arr.push(new Promise(function(resolve1, reject1) {
				(function(path1,resolve1, reject1){
					uni.common.post(
						uni.common.apiServer + 'yin', {
							type: 'get_obs_key', //获取有多少题
							path:path1
						},
						true,
						(res) => {
							if (res.code == 200) {
								uni.uploadFile({
										url: res.data.url, //用你自己的 bucket 名替换星号
										filePath: data.tempFilePaths[i],
										name: 'file',
										formData: {
											'key': path1,
											'AccessKeyId': res.data.Accesskeyid,
											'Policy': res.data.Policy,
											'x-obs-acl': 'public-read',
											'content-type': '',
											'Signature': res.data.Signature
										},
										success() {
											true_arr.push(path1)
											resolve1()
										},fail() {
											reject1();
										}
									}
								)
							} else {
								reject1('上传失败');
							}
						},
						(e) => {
							console.log(e);
						}
					);
				})(path,resolve1, reject1)
			}))
		}
		Promise.all(linshi_arr).then((res) => {
			resolve( true_arr)
		}).catch((err) => {
			console.log(err);
			reject('上传失败');
		})
	});
}
相关推荐
咖啡の猫1 小时前
Shell脚本-for循环应用案例
前端·chrome
百万蹄蹄向前冲4 小时前
Trae分析Phaser.js游戏《洋葱头捡星星》
前端·游戏开发·trae
anlogic4 小时前
Java基础 8.18
java·开发语言
沐知全栈开发4 小时前
WebForms XML 文件详解
开发语言
朝阳5814 小时前
在浏览器端使用 xml2js 遇到的报错及解决方法
前端
GIS之路4 小时前
GeoTools 读取影像元数据
前端
阿巴~阿巴~5 小时前
冒泡排序算法
c语言·开发语言·算法·排序算法
ssshooter5 小时前
VSCode 自带的 TS 版本可能跟项目TS 版本不一样
前端·面试·typescript
你的人类朋友5 小时前
【Node.js】什么是Node.js
javascript·后端·node.js
Jerry5 小时前
Jetpack Compose 中的状态
前端