2012-09-19 58 views
1

做對象文字塊嗎?我正在嘗試將其他函數的結果存儲在我的分頁助手類中,並且由於未返回回調,因此我的Mocha測試因超時而失敗。如果我從我的所有變量(即local.countcount)中刪除local,並刪除持有該回調的.then,並將它放在第一個.then中,它突然生效。在node.js中做對象文字塊嗎

render: (model, query, cb) => 
    self = @ 
    count = 0 
    local = {} 

    count_promise = @query_count model, query 

    count_promise.then (c) -> 
     local.count = c 
     local.pages = self.pages(count) 
     local.prev_class = self.prev_class(pages) 
     local.next_class = self.next_class(pages) 
     local.prev_page = self.prev_page(pages) 
     local.next_page = self.next_page(pages) 
    .then -> 
     cb({num_pages: local.pages, current_page: self.page, prev_class: local.prev_class, next_class: local.next_class, prev_page: local.prev_page, next_page: local.next_page, results: {}}) 

    .fail (err) -> 
     throw new Error(err) 

    return 

編輯

我加的console.log回調之前,是否有任何露面。

.then -> 
     console.log local 
     cb({num_pages: local.pages, current_page: self.page, prev_class: local.prev_class, next_class: local.next_class, prev_page: local.prev_page, next_page: local.next_page, results: {}}) 

什麼也沒有出現。

使用興農和柴此特定方法的摩卡測試代碼斷言:

describe 'PaginationHelper', -> 

    describe 'Pagination Helper Initializes', -> 
    it 'accepts page', -> 
     assert.ok(new PaginationHelper(2)) 

    describe 'method testing', -> 

    describe 'render method', -> 
     ph = new PaginationHelper(2) 
     result = {} 

     before (done) -> 
      ph.render BlogModel, {}, (res) -> 
      result = res 
      done() 

     it 'returns object', -> 
      assert.isObject(result) 

     it 'returns object with num_pages', -> 
      assert.property(result, "num_pages") 

     it 'returns object with current_page', -> 
      assert.property(result, 'current_page') 

     it 'returns object with prev_class', -> 
      assert.property(result, 'prev_class') 

     it 'returns object with next_class', -> 
      assert.property(result, 'next_class') 

     describe 'render output testing', -> 
      result = undefined 

      before (done) -> 
      ph = new PaginationHelper(1) 
      ph.render BlogModel, {}, (res) -> 
       result = res 
       done() 

      it 'returns a number for num_pages', -> 
      assert.isNumber(result.num_pages) 

      it 'returns a number for current_page', -> 
      assert.isNumber(result.current_page) 

      it 'returns string for prev_class', -> 
      assert.isString(result.prev_class) 

      it 'returns string for next_class', -> 
      assert.isString(result.next_class) 

該測試的結果輸出:

Error: Command failed: ✖ 1 of 35 tests failed: 

    1) PaginationHelper method testing render method "before all" hook: 
    Error: timeout of 2000ms exceeded 
+1

你有例外訪問不定值財產,並永遠不會執行回調函數時,因此摩卡超時 –

+0

感謝您的答覆。我認爲地方可以保留從第一個到第二個的價值,然後我將插入更多的承諾函數。爲什麼高級作用域中的「本地」對象字面值不像正常的函數變量那樣異步工作? – BioDevMike

+0

我以前的回答可能是錯的。你能發佈最小自包含的例子嗎?我懷疑你的第二個'then'沒有被調用(你可以在'cb'之前放置'console.log'嗎? –

回答

1

我想出問題。我直到深入研究Q promises API,才正確理解了可敬的鏈接。在我沒有改正的變量中也有愚蠢的錯誤。編碼雖然超級累,但並不酷。下面是代碼應該是什麼樣子:

render: (model, query, cb) => 
    self = @ 

    count_promise = @query_count model, query 

    count_promise.fail (err) -> 
     throw new Error(err) 
    .then (c) -> 
     defer = Q.defer() 
     local = {} 
     local.count = c 
     local.pages = self.pages(local.count) 
     local.prev_class = self.prev_class(local.pages) 
     local.next_class = self.next_class(local.pages) 
     local.prev_page = self.prev_page(local.pages) 
     local.next_page = self.next_page(local.pages) 
     defer.resolve(local) 
     return defer.promise 
    .then (local) -> 
     cb({num_pages: local.pages, current_page: self.page, prev_class: local.prev_class, next_class: local.next_class, prev_page: local.prev_page, next_page: local.next_page, results: {}}) 
    return