2016-10-20 12 views
2

でPromise.allを使用します。私はPromise.all のように使用したい:変更方法は、私は次のような方法を持っているtypescriptです

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を使用するようにコードを手直しする必要があります。不明な質問に申し訳ありません。

答えて

2

その後、集約された約束である、アレイ上Promise.allを呼び出し、結果を返し、配列内のあなたの約束を収集します。私は活字体に大きなないんだけど

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); 
} 

、あなたは調整する必要があり配列の宣言。関数の戻り値の型をvoidからPromiseに変更しました。

1

ループを使用するのではなく、timesArrayを作成することができます。次にmapと入力してください。

public static zoomInMap(times: number): Promise<void> { 
    return Promise.all(new Array(times).fill().map(() => { 

    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(); 

    return browser.sleep(Config.ZOOM_ANIMATION_TIMEOUT).then(() => { 
    // console.log('Map Zoomed In'); 
    }); 

    })); 
} 
関連する問題