如何重写此代码,以获得所需的 o/p。我想在填写数据后使用 AgentReply 对象。在开关机箱中,此对象包含数据。但一旦出门,它又是空的。明白是因为异步,但是我该怎么办,一旦有数据就能够使用"AgentReply"。
$scope.ActionItems = function (actionItem) {
var AgentReply = {};
switch (actionItem) {
case "SendOTP":
var SentStatus = "";
DataFactory.SendOTP('39487539847')
.then(function (response) {
SentStatus = JSON.parse(JSON.parse(response.data));
SendOTPFailed();
}, function (error) {
});
break;
}/*End of switch*/
function SendOTPFailed(){
if (SentStatus == "200") {
AgentReply = {
IsCustomer: false,
UserText: "Request Failed.",
}
}
}
if (Object.keys(AgentReply).length > 0) {
//do something with AgentReply
}
}
只需将一个函数传递到AgentReply
可用的地方,并在下面定义它,即:
$scope.ActionItems = function (actionItem) {
var AgentReply = {};
switch (actionItem) {
case "SendOTP":
var SentStatus = "";
DataFactory.SendOTP('39487539847')
.then(function (response) {
SentStatus = JSON.parse(JSON.parse(response.data));
if (SentStatus == "200") {
AgentReply = {
IsCustomer: false,
UserText: "Request Failed.",
}
}
doSomethingWithAgentReply(AgentReply);
}, function (error) {
});
break;
}
console.log(AgentReply); //null here
function doSomethingWithAgentReply(reply) {
if (Object.keys(reply).length > 0) {
//do something with AgentReply
}
}
}
如果需要使用此代码:
if (Object.keys(AgentReply).length > 0) {
//do something with AgentReply
}
}
.then()
功能外:
DataFactory.SendOTP('39487539847')
.then(function (response) {
})
你可以试试这个:
$scope.ActionItems = function (actionItem) {
var def = jQuery.Deferred();
var AgentReply = {};
switch (actionItem) {
case "SendOTP":
var SentStatus = "";
DataFactory.SendOTP('39487539847')
.then(function (response) {
SentStatus = JSON.parse(JSON.parse(response.data));
if (SentStatus == "200") {
AgentReply = {
IsCustomer: false,
UserText: "Request Failed.",
}
def.resolve(AgentReply);
}
console.log(AgentReply); //available here
}, function (error) {
def.reject(error);
});
return def.promise();
break;
}
//console.log(AgentReply); //null here
//if (Object.keys(AgentReply).length > 0) {
//do something with AgentReply
// }
//}
// This is unusable in this case.
用法是:
var someActionItem = 'SomeActionItemInfo';
$scope.ActionItems(someActionItem)
.then(function(agentReply) {
if (Object.keys(agentReply).length > 0) {
//do something with agentReply
}
}, function(error));
编辑:
$scope.ActionItems
是相同的功能。当你使用承诺时会发生什么?
首先定义 deffer 对象。 var def = jQuery.Deferred()
.这个对象在jQuery,但所有支持框架/库都承诺以相同的方式工作。
如您所见,您返回def.promise()
.这是包含.then
属性的对象。由于该对象,您可以使用$scope.ActionItems().then()
方法。这实际上使def.promise()
.
在您的异步代码中(此代码消耗一些时间并且不会立即执行),您定义def.resolve()
或def.reject()
。
工作完成后。您调用def.resolve(withSomeData)
,这将激活.then()
方法到$scope.ActionItems
。
例如:
var foo = null;
function doPromise() {
var def = jQuery.Deferred();
setTimeout(function(){
foo = 2;
def.resolve(foo + 1) // This will call the .then() method with foo + 1
}, 500);
return def.promise();
}
doPromise();
console.log(foo) // foo = null here. cuz the function waiting 500ms.
// Here the .then() method will be executed after ~500+ ms.
doPromise().then(function(fooValue) {
console.log(fooValue) // foo value = 3 here. cuz function is done
});