promise chaining vs promise.all

前端 未结 1 1700
醉梦人生
醉梦人生 2021-01-29 02:44

i have a task to enter the notification date using protractor where i need to clear contents before entering so i have came up with this code

 this.Then(/^I sh         


        
相关标签:
1条回答
  • 2021-01-29 03:16

    Protractor has its own promise manage mechanism called control flow, To understand control flow simply, you can think it is a queue.

    When nodejs execute your Protractor script line by line, if the expression in the line return a promise, control flow will add the promise into the queue.

    After all lines execute done, you will get a promise queue, at this time point your testing had not finish yet, because control flow will make your testing to wait all promise in the queue be executed. Now control flow will pop a promise from the queue and execute and wait it complete, then next promise.

    So with such mechanism, your script can be executed as the order as you write down in file. Actually what control flow did is more complex than I said here.

    You no need to use nested then chain in your case, your code like the callback pyramid, not represent the advantage of promise (promise is to resolve callback pyramid). Your code can be simple as below:

    const d = new Date();
    //input month
    orderCheckOutPage.pageElements.recipientNotificationDateMonth.clear();
    orderCheckOutPage.pageElements.recipientNotificationDateMonth.sendKeys(d.getMonth() + 1);
    //input day
    orderCheckOutPage.pageElements.recipientNotificationDateDay.clear();
    orderCheckOutPage.pageElements.recipientNotificationDateDay.sendKeys(d.getDate());
    //input year
    orderCheckOutPage.pageElements.recipientNotificationDateYear.clear();
    orderCheckOutPage.pageElements.recipientNotificationDateYear.sendKeys(d.getFullYear());
    

    For your case, no need to use promise.all(), because all interaction of your code not to get some value from page. I will give an example to help you learn in which case it's better to use promise.all():

    Assume i have a page and it display a price and a amount. I need to calculate the fee by price * amount.

    Use nested then chain:

    var fee = ele_price.getText().then(function(price){
    
        return ele_amount.getText().then(function(amount){
            return price * amount;
        });
    });
    
    fee.then(function(fee){
        console.log(fee);
    });
    

    Use promise.all():

    var fee = promise.all([
      ele_price.getText(),
      ele_amount.getText()
    ])
    .then(function(datas){
        var price = datas[0];
        var amount = datas[1];
        return price * amount;
    });
    
    fee.then(function(fee){
        console.log(fee);
    });
    

    So use promise.all(), one then() is enough. This makes your code more readable than nested then chain.

    Hope you now understand why no need to use promise.all() in your case.

    0 讨论(0)
提交回复
热议问题