我有以下方法:
public static zoomInMap(times: number): void {
for (let i = 0; i < times; i++) {
let zoomInButton = element(by.css('#main > cc-map > div.google-map-base- container-inner > div > div.gmnoprint.gm-bundled-control.gm-bundled-control-on-bottom > div:nth-child(1) > div > div:nth-child(1)'));
zoomInButton.click();
browser.sleep(Config.ZOOM_ANIMATION_TIMEOUT).then(() => {
// console.log('Map Zoomed In');
});
}
}
我想让它返回一个 Promise 。我想使用 Promise.all 喜欢:
public static zoomInMap(times: number): Promise<any> {
return Promise.all( ? ) // ? I do not know how to do it
for (let i = 0; i < times; i++) {
let zoomInButton = element(by.css('#main > cc-map > div.google-map-base-container-inner > div > div.gmnoprint.gm-bundled-control.gm-bundled-control-on-bottom > div:nth-child(1) > div > div:nth-child(1)'));
zoomInButton.click();
browser.sleep(Config.ZOOM_ANIMATION_TIMEOUT).then(() => {
// console.log('Map Zoomed In');
});
}
}
我应该如何修改代码以使用 Promise.all 。抱歉问了个蹩脚的问题。
最佳答案
将您的 Promise 收集到一个数组中,然后在该数组上调用 Promise.all
并返回结果,即聚合的 Promise:
public static zoomInMap(times: number): Promise {
let promises = [];
for (let i = 0; i < times; i++) {
let zoomInButton = element(by.css('#main > cc-map > div.google-map-base- container-inner > div > div.gmnoprint.gm-bundled-control.gm-bundled-control-on-bottom > div:nth-child(1) > div > div:nth-child(1)'));
zoomInButton.click();
promises.push(browser.sleep(Config.ZOOM_ANIMATION_TIMEOUT).then(() => {
// You can do something here if you like, or remove the `then`
}));
}
return Promise.all(promises);
}
我不太擅长 TypeScript,你可能需要调整数组的声明。我已将函数的返回类型从 void
更改为 Promise
。
关于javascript - 更改方法以在 typescript 中使用 Promise.all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40158448/