使用 Promises 的异步 POST 请求
async POST request using Promises
我正在构建一个简单的上传器,我之前已经构建了很多,但是 none 使用承诺,我 运行 遇到了问题。当我调用我的承诺时,我需要绑定我的 "uploaded files" 以在下面的代码中说出一个表单数据对象又名 data.append('xls', e.dataTransfer.files[0])
,但是我无法再访问 e
事件,所以我无法访问它并将其绑定到表单 data
对象。
错误 - Uncaught (in promise) ReferenceError: e is not defined
<!DOCTYPE html>
<html>
<head>
<script>
//HELPER FUNCTION
events = (o,type,handle)=>o.addEventListener(type,handle);
listen = {
drag: ['dragenter','dragover','drop','dragleave'],
async: ['readystatechange','loadstart','progress','abort','error','load','timeout','loadend']
};
//PROMISES
executor = {
upload: (resolve,reject) =>{
var data = new FormData();
var client = new XMLHttpRequest();
for(prop of listen.async){ events(client, prop, callback.upload[prop]);}
//THIS IS WHERE MY PROBLEM LIES
data.append('xls', e.dataTransfer.files[0]);
client.addEventListener("load", callback.upload);
client.open("POST", "/core/upload.php");
client.send(data);
}
};
//PROMISE CALLBACKS
resolve = (value)=> console.log(value);
reject = (reason)=> console.log(reason);
//EVENT HANDLES
handles = {
upload: {
dragenter: (e)=> e.target.classList.remove('emboss'),
dragover: (e)=> e.preventDefault(),
drop: (e)=> {
e.preventDefault();
var p = new Promise(executor.upload);
console.log(p);
},
dragleave: (e)=> e.target.classList.add('emboss')
}
};
//ASYNC CALLBACKS
callback = {
upload: {
readystatechange: (e)=> console.log(e.target.readyState),
loadstart: (e)=> console.log('loadstart'),
progress: (e)=> console.log('progress'),
abort: (e)=> console.log('abort'),
error: (e)=> console.log('error'),
load: (e)=> console.log('load'),
timeout: (e)=> console.log('timeout'),
loadend: (e)=> console.log('loadend')
}
};
//INITIALIZATION
init=()=>{
var dropbox = document.getElementById('dropbox');
for(prop of listen.drag){ events(dropbox, prop, handles.upload[prop]);}
};
events(document,'DOMContentLoaded', init);
</script>
</head>
<body>
<div id='dropbox' class='fa fa-file-excel-o fa-4x emboss'></div>
</body>
</html>
基本问题是将从事件对象获得的参数传递给 Promise 的执行器函数,注意执行器被 Promise 构造函数同步调用。
一种方法可能是创建一个将参数保存在闭包中的执行器工厂函数,但是当将文件参数值存储在执行器可以访问的位置时,这可能会过大杀伤力应该同样有效。
设置一个 属性 的,比如说,上传函数对象,比如:
executor = {
upload: (resolve,reject) =>{
var data = new FormData();
var client = new XMLHttpRequest();
for(prop of listen.async){ events(client, prop, callback.upload[prop]);}
data.append('xls', executor.upload.files);
.... // etc
并在创建承诺之前设置参数,
. . .
executor.upload.files = e.dataTransfer.files[0]`;
var p = new Promise(executor.upload);
. . .
我正在构建一个简单的上传器,我之前已经构建了很多,但是 none 使用承诺,我 运行 遇到了问题。当我调用我的承诺时,我需要绑定我的 "uploaded files" 以在下面的代码中说出一个表单数据对象又名 data.append('xls', e.dataTransfer.files[0])
,但是我无法再访问 e
事件,所以我无法访问它并将其绑定到表单 data
对象。
错误 - Uncaught (in promise) ReferenceError: e is not defined
<!DOCTYPE html>
<html>
<head>
<script>
//HELPER FUNCTION
events = (o,type,handle)=>o.addEventListener(type,handle);
listen = {
drag: ['dragenter','dragover','drop','dragleave'],
async: ['readystatechange','loadstart','progress','abort','error','load','timeout','loadend']
};
//PROMISES
executor = {
upload: (resolve,reject) =>{
var data = new FormData();
var client = new XMLHttpRequest();
for(prop of listen.async){ events(client, prop, callback.upload[prop]);}
//THIS IS WHERE MY PROBLEM LIES
data.append('xls', e.dataTransfer.files[0]);
client.addEventListener("load", callback.upload);
client.open("POST", "/core/upload.php");
client.send(data);
}
};
//PROMISE CALLBACKS
resolve = (value)=> console.log(value);
reject = (reason)=> console.log(reason);
//EVENT HANDLES
handles = {
upload: {
dragenter: (e)=> e.target.classList.remove('emboss'),
dragover: (e)=> e.preventDefault(),
drop: (e)=> {
e.preventDefault();
var p = new Promise(executor.upload);
console.log(p);
},
dragleave: (e)=> e.target.classList.add('emboss')
}
};
//ASYNC CALLBACKS
callback = {
upload: {
readystatechange: (e)=> console.log(e.target.readyState),
loadstart: (e)=> console.log('loadstart'),
progress: (e)=> console.log('progress'),
abort: (e)=> console.log('abort'),
error: (e)=> console.log('error'),
load: (e)=> console.log('load'),
timeout: (e)=> console.log('timeout'),
loadend: (e)=> console.log('loadend')
}
};
//INITIALIZATION
init=()=>{
var dropbox = document.getElementById('dropbox');
for(prop of listen.drag){ events(dropbox, prop, handles.upload[prop]);}
};
events(document,'DOMContentLoaded', init);
</script>
</head>
<body>
<div id='dropbox' class='fa fa-file-excel-o fa-4x emboss'></div>
</body>
</html>
基本问题是将从事件对象获得的参数传递给 Promise 的执行器函数,注意执行器被 Promise 构造函数同步调用。
一种方法可能是创建一个将参数保存在闭包中的执行器工厂函数,但是当将文件参数值存储在执行器可以访问的位置时,这可能会过大杀伤力应该同样有效。 设置一个 属性 的,比如说,上传函数对象,比如:
executor = {
upload: (resolve,reject) =>{
var data = new FormData();
var client = new XMLHttpRequest();
for(prop of listen.async){ events(client, prop, callback.upload[prop]);}
data.append('xls', executor.upload.files);
.... // etc
并在创建承诺之前设置参数,
. . .
executor.upload.files = e.dataTransfer.files[0]`;
var p = new Promise(executor.upload);
. . .