首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >把承诺变成整数

问把承诺变成整数
EN

Stack Overflow用户
提问于 2015-08-20 19:40:32
回答 2查看 1.4K关注 0票数 2

这也许是一个愚蠢的问题,但是我很难找到我需要的答案。我有个中继器计数的测试。我想返回count变量,这样我就可以将它传递给另一个测试,以便在for循环中使用。

我试过的是:

​

代码语言:javascript
复制
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;
};

​

这就是吐出outStarted的东西

代码语言:javascript
复制
.{ 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-20 20:03:19

由于.count()是异步的,所以不可能从countsInitialBookings返回整数。相反,您应该返回承诺,然后您可以在以后使用该承诺来获得值。

代码语言:javascript
复制
this.countsInitialBookings = function() {
  var j = 0;
  browser.driver.sleep(3000);
  bookingLists = element.all(by.repeater('booking in bookingsCtrl.bookings'));
  return bookingLists.count()
};

稍后..。

代码语言:javascript
复制
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.
票数 3
EN

Stack Overflow用户

发布于 2015-08-20 22:09:47

就像凯文·B的回答一样。

你正在返回一个“承诺骨架”对象。管理你的承诺链是很重要的,以确保你是1)。返回异步调用2。))返回所需的任何值。)把承诺锁在正确的地方。

虽然我同意Kevin的回答,但让预订成为全球性的,或者改变它的范围,可能会导致另一种解决方案。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32126948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档