把诺言变成了一个int

时间:2015-08-20 19:40:33

标签: angularjs protractor

这可能是一个愚蠢的问题但是我找不到我需要的答案。我有一个计算转发器的测试。我想返回count变量,所以我可以把它传递给另一个在for循环中使用的测试。

我尝试了什么:

this.countsInitialBookings = function() {
  var j = 0;
  browser.driver.sleep(3000);
  bookingLists = element.all(by.repeater('booking in bookingsCtrl.bookings'));
  bookingLists.count().then(function(count) {
    //have also tried making bookingCount global and setting value to 0   
    var bookingCount = count;
  });
  return bookingCount;
};

这就是spits outStarted

.{ closure_uid_966269928: 651,
  flow_: 
   { events_: {},
     closure_uid_966269928: 1,
     activeFrame_: 
      { events_: {},
        closure_uid_966269928: 642,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: false,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     schedulingFrame_: 
      { events_: {},
        closure_uid_966269928: 642,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: false,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     shutdownTask_: null,
     eventLoopTask_: null,
     hold_: 
      { _idleTimeout: 2147483647,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 99601735,
        _onTimeout: [Function: wrapper],
        _repeat: true },
     yieldCount_: 1 },
  stack_: null,
  parent_: 
   { closure_uid_966269928: 649,
     flow_: 
      { events_: {},
        closure_uid_966269928: 1,
        activeFrame_: [Object],
        schedulingFrame_: [Object],
        shutdownTask_: null,
        eventLoopTask_: null,
        hold_: [Object],
        yieldCount_: 1 },
     stack_: null,
     parent_: 
      { closure_uid_966269928: 647,
        flow_: [Object],
        stack_: null,
        parent_: [Object],
        callbacks_: [Object],
        state_: 'pending',
        handled_: true,
        pendingNotifications_: false,
        value_: undefined },
     callbacks_: [ [Object] ],
     state_: 'pending',
     handled_: true,
     pendingNotifications_: false,
     value_: undefined },
  callbacks_: null,
  state_: 'pending',
  handled_: false,
  pendingNotifications_: false,
  value_: undefined }
.


2 specs, 0 failures
Finished in 17.516 seconds
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 passed

Process finished with exit code 0

2 个答案:

答案 0 :(得分:3)

由于.count()是异步的,因此无法从countsInitialBookings返回整数。相反,您应该返回承诺,然后您可以稍后使用承诺来获取值。

this.countsInitialBookings = function() {
  var j = 0;
  browser.driver.sleep(3000);
  bookingLists = element.all(by.repeater('booking in bookingsCtrl.bookings'));
  return bookingLists.count()
};

稍后......

this.countsInitialBookings().then(function (count) {
    console.log(count);
});
// do not attempt to get `count` out here, it's simply not possible. 
// It will only be accessible inside the above callback.

答案 1 :(得分:2)

类似于Kevin B的回答......

您正在返回“Promise Skeleton”对象。以一种确保你是1.)返回异步调用的方式管理你的承诺链非常重要2.)返回你需要的任何值3.)并在正确的地方链接承诺。

虽然我同意凯文的回答,让预订全球化,或改变其范围,但可能会导致另一种解决方案。