AngularJS 中有关异步、承诺和链接的问题
Questions about async, promises, and chaining in AngularJS
我一直在努力自学 promises 的基础知识、异步调用以及如何在 AngularJS 中链接它们。目前我正在维护一个喜欢在任何地方使用它们的应用程序,作为一个新手,至少可以说它们真的让人不知所措。
首先,我在服务器端 (node.js) 中有这段代码可以检索经理的直接下属列表。之前的开发者就是这样做的,所以我以此为指导:
exports.findDirectReports = function (req, res) {
var empnum = req.params.empnum;
queries.getDirectReports(empnum)
.then(function (users) {
return res.json(users);
})
.catch(function (err) {
handleError(res, err);
});
};
我理解(或自以为理解):
queries.getDirectReports
将首先执行并且 return 列表作为承诺。
- 已解决的承诺将 return 编辑在
then()
。
return res.json(users)
结果将被传回给调用它的人以用于其他操作。
我的问题是获得已解决的承诺。我知道我不会马上得到结果,因为它是 async
。但是我有一个 find()
将这些结果用作条件,每次检查结果时我总是得到 null
或 []
.
- 有没有办法立即得到它们?
- 如果有none,我如何在结果准备好后立即执行我的其他功能?
这是有问题的功能(也是服务器端 node.js):
exports.downloadEmployee = function (req, res) {
/*
"queries" - alias for another server-side node.js (not really sure what to call
this exactly) that contains the entire getDirectReports fxn.
*/
queries.getDirectReports(empnum)
.then(function (users) {
EmployeeInfo.find({}, function (err, results) {
var tempCtr = [];
if (err) { return err; }
/*
Use results of getDirectReports here as condition
where if employee exists, program will execute
_.forEach below.
*/
_.forEach(results, function (item) {
tempCtr.push({
employeeID: item.employeeID,
employeeName: item.employeeName
});
});
return res.json(tempCtr);
});
})
.catch(function (err) {
handleError(res, err);
});
}
我在 SO 的某个地方看到我需要利用 callbacks
,但我真的不明白如何。一些代码示例与我之前尝试过的代码示例相似(但没有成功)。
任何答案将不胜感激。
谢谢。
您需要return
来自函数的承诺
exports.downloadEmployee = function (req, res) {
return queries.getDirectReports(empnum)
.then(function (users) {
...
return res.json(user);
})
.catch(function (err) { ... });
}
Promise 是异步的,所以没有办法解决这个问题 "immediately" 并且您需要处理从中调用此函数的 promise 的解决方案。
我一直在努力自学 promises 的基础知识、异步调用以及如何在 AngularJS 中链接它们。目前我正在维护一个喜欢在任何地方使用它们的应用程序,作为一个新手,至少可以说它们真的让人不知所措。
首先,我在服务器端 (node.js) 中有这段代码可以检索经理的直接下属列表。之前的开发者就是这样做的,所以我以此为指导:
exports.findDirectReports = function (req, res) {
var empnum = req.params.empnum;
queries.getDirectReports(empnum)
.then(function (users) {
return res.json(users);
})
.catch(function (err) {
handleError(res, err);
});
};
我理解(或自以为理解):
queries.getDirectReports
将首先执行并且 return 列表作为承诺。- 已解决的承诺将 return 编辑在
then()
。 return res.json(users)
结果将被传回给调用它的人以用于其他操作。
我的问题是获得已解决的承诺。我知道我不会马上得到结果,因为它是 async
。但是我有一个 find()
将这些结果用作条件,每次检查结果时我总是得到 null
或 []
.
- 有没有办法立即得到它们?
- 如果有none,我如何在结果准备好后立即执行我的其他功能?
这是有问题的功能(也是服务器端 node.js):
exports.downloadEmployee = function (req, res) {
/*
"queries" - alias for another server-side node.js (not really sure what to call
this exactly) that contains the entire getDirectReports fxn.
*/
queries.getDirectReports(empnum)
.then(function (users) {
EmployeeInfo.find({}, function (err, results) {
var tempCtr = [];
if (err) { return err; }
/*
Use results of getDirectReports here as condition
where if employee exists, program will execute
_.forEach below.
*/
_.forEach(results, function (item) {
tempCtr.push({
employeeID: item.employeeID,
employeeName: item.employeeName
});
});
return res.json(tempCtr);
});
})
.catch(function (err) {
handleError(res, err);
});
}
我在 SO 的某个地方看到我需要利用 callbacks
,但我真的不明白如何。一些代码示例与我之前尝试过的代码示例相似(但没有成功)。
任何答案将不胜感激。
谢谢。
您需要return
来自函数的承诺
exports.downloadEmployee = function (req, res) {
return queries.getDirectReports(empnum)
.then(function (users) {
...
return res.json(user);
})
.catch(function (err) { ... });
}
Promise 是异步的,所以没有办法解决这个问题 "immediately" 并且您需要处理从中调用此函数的 promise 的解决方案。