我目前正在为我的电子应用建立许可证系统。但是我在以下方面还没有成功:
- 保证提示用户输入(电子提示模块)
- 紧接之后对服务器进行ajax调用
- 评估响应(来自服务器端脚本的1 \ n或0 \ n)
头痛的是在从提示符(结构化为承诺)中获取用户输入并实际上等待它完成之后运行ajax调用。
直到现在,我仍在尝试重新构建自己的诺言,例如:
(1)
prompt().
then(ajax()).
then(evaluate()).
catch()
(2)
prompt().
then(ajax().
then(evaluate())).
catch()
这是我当前所在的位置,ajax函数不会返回要传递的任何内容。
首先是提示,它会相应地工作,并返回其值。
function activeValidation(){
try{
prompt({
title: "Enter key",label: "Enter your key",value: "",alwaysOnTop: true,autoHideMenuBar: true,inputAttrs: {
type: 'text'
},type: "input"
})
头痛属于这一部分,即ajax。
.then(function(userinput){
return new Promise(function(resolve,userinput){
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
resolve(req.responseText);
}
}
req.open("GET","someCGIscript?key="+userinput,true);
req.send();
}).then(function(response){
if(response == "1\n"){
runProgram();
}
else{
}
}).catch(function(e){
console.error(e);
terminate();
})});
}
catch(e){
console.log(e.name);
console.log(e.message);
terminate();
}
}
我尝试从resolve(req.responseText);
打印ajax Promise的值,但是在以下段中返回undefined。
此后,我试图重组两个诺言(嵌套,并作为第一个诺言的.then),但是没有运气。
我相信这是正确构建诺言的问题,但是我仍然是Promises的新手,至少在js :)中。