js异步变同步、同步变异步

1.异步变同步 


/**
 * bloburl转file对象
 * @param {*} blobUrl 
 * @param {*} fileName 
 */
function blobUrlToFile(blobUrl, fileName) {
	let that = this;
	return new Promise((resolve, reject) => {
		let xhr = new XMLHttpRequest();
		xhr.open('GET', blobUrl, true);
		xhr.responseType = 'blob';
		xhr.onload = function(e) {
			if (this.status == 200) {
				let myBlob = this.response;
				// console.log('myBlob', e);
				let files = new window.File([myBlob], fileName, {
					type: myBlob.type
				}); // myBlob.type 自定义文件名
				resolve(files);
			} else {
				reject(false);
			}
		};
		xhr.send();
	});


}
await blobUrlToFile(url, name).then(file => {
					files.push(file);
				});

2.同步变异步

function blobUrlToFile(blobUrl, fileName) {
	let that = this;
	return new Promise((resolve, reject) => {
	
			//成功
				resolve(true);
			//失败
				reject(false);
			
	});


}

猜你喜欢

转载自blog.csdn.net/wcdunf/article/details/126102281