我有一个要处理的对象列表。该对象被传递给一个承诺函数,该函数执行此操作并解析返回。根据先前缓存的值,该过程可能是即时的,也可能不是。如果已经有计算值,它会立即解析到它。否则,它会计算。现在我遇到的问题是在计算第一个对象的状态之前将下一个对象传递给承诺:
let people = [
{groupId: 1, name: 'Jessica Coleman', status: 'Unknown', id:1}
{groupId: 1, name: 'Eric Tomson', status: 'Unknown', id:2}
{groupId: 1, name: 'Samuel Bell', status: 'Unknown', id:3}
];
Run Code Online (Sandbox Code Playgroud)
现在我想绝对等待承诺在循环期间解决,即使承诺需要一分钟来计算实例。同一组中的所有人员都具有相同的状态。因此,promise 检查是否已经计算了一个组。如果是,则返回它。否则,它会计算。这就是问题所在。杰西卡1号还没说完,其他人就通过了。
people.map(function(person) {
// return the promise to array
this.calculatorService
.getStatus(person)
.then(function(res) {
person.status = res;
});
});
Run Code Online (Sandbox Code Playgroud) 更新:
似乎以下作品:
let gaService = TestBed.get(GAService);
Run Code Online (Sandbox Code Playgroud)
并监视 gaService。
我对我在这里缺少的东西真的很空白。该服务将数据发送到谷歌分析,所以我必须在测试期间模拟它。服务本身是:
import { Injectable } from '@angular/core';
import { CoreService} from "./core.service";
@Injectable()
export class GAService {
constructor(
public coreService: CoreService
) { }
public sendException (
description: string,
isFatal: boolean
) : Promise<any> {
let promise = new Promise((resolve, reject) => {
(<any>window).ga('send', 'exception', {
'exDescription': description,
'exFatal': isFatal
});
});
return promise;
}
}
Run Code Online (Sandbox Code Playgroud)
它被这样嘲笑:
import { Injectable } from "@angular/core";
@Injectable()
export class MockGAService {
constructor() {}
sendException(
description: …Run Code Online (Sandbox Code Playgroud)