为什么JavaScript Promises中的计算变量无法在外部识别

时间:2016-05-12 08:04:56

标签: javascript angularjs

var appCounts = 0;
applicationsService.getApplicationCountByJob(job.id).then((appcount) => {
    appCounts = appCounts + appcount;
});
company.applications = appCounts;

appCount在company.applications = appCounts;

中失去了价值

请让我知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

你的函数getApplicationCountByJob可能是异步的,意思是

company.applications = appCounts;

之前执行
appCounts = appCounts + appcount;

进入另一个,然后让它工作:

var appCounts = 0;
applicationsService.getApplicationCountByJob(job.id)
.then((appcount) => {
    appCounts = appCounts + appcount;
})
.then(() => {
  company.applications = appCounts;
});