柴承诺不等待承诺得到履行

时间:2016-05-29 20:06:31

标签: javascript promise mocha chai chai-as-promised

我的控制台输出。请注意,控制台日志不按顺序排列(1,3,4,2而不是1,2,3,4)

![enter image description here

此处代码

app.controller('ctrlA', $scope, $http, $location, $sce) {
    app.db_read($scope, $http, {param: 1}, $sce, 
         function() {// succeeded},
         function() {// failed}
    )
}

app.controller('ctrlB', $scope, $http, $location, $sce) {
    app.db_read($scope, $http, {param: 1}, $sce, 
         function() {// succeeded},
         function() {// failed}
    )
}
...
app.db_read = function($scope, $http, payload, $location, $sce) {
    $http.post('url', {request: 'read', data: payload})
         .then(function(data) {$scope.data = data},
         function(error) {$scope.displayError = $sce.trustAsHtml(error)}
    )
}

2 个答案:

答案 0 :(得分:1)

这结果是一个愚蠢的错字。我输入了fullfilled而不是fulfilled

答案 1 :(得分:-1)

我怀疑你从Chai那里得到了副作用。首先尝试不使用它进行测试,例如:

const assert = require('assert');

it('can store file', () => {
    console.log('1) file storage start')
    return filestore.store.q(file).then(() => {
      // Promise should have fulfilled. Nothing more to do.
      // Using should and chai after this is probably causing the problem.
      // But you should really add some sort of assertion here to
      // be able to detect regressions.
      console.log('2) file storage done')
    });
  });

  describe('block number', () => {
    let blockNumber;

    beforeEach(() => {
      console.log('3) check blockNumber')
      return web3.Q.all([
        web3.eth.getBlockNumber.q().then((_blockNumber) => {
          blockNumber = _blockNumber
        }),
        web3.eth.getMining.q().then((isMining) => {
        })
      ])
    })

    it('can retreive files block number', () => {
      console.log('4) retreive')
      return filestore.getBlockNumber.q(fileHash)
        .then((result) => {
          // I'm not sure if assert.equal will work with big numbers.
          // You might need a different comparator here.
          assert.equal(result, blockNumber, 'should equal the blocknumber));
        });
    });
  });

Mocha知道如何处理返回的Promise,所以真的不需要Chai。这是不必要的糖。

相关问题