Javascript 柴如承诺,不等待承诺兑现
我的控制台的输出。请注意,控制台日志顺序错误(1,3,4,2而不是1,2,3,4) 代码在这里Javascript 柴如承诺,不等待承诺兑现,javascript,promise,mocha.js,chai,chai-as-promised,Javascript,Promise,Mocha.js,Chai,Chai As Promised,我的控制台的输出。请注意,控制台日志顺序错误(1,3,4,2而不是1,2,3,4) 代码在这里 it('can store file', () => { console.log('1) file storage start') return filestore.store.q(file).then(() => { console.log('2) file storage done') }).should.eventually.be.fullf
it('can store file', () => {
console.log('1) file storage start')
return filestore.store.q(file).then(() => {
console.log('2) file storage done')
}).should.eventually.be.fullfilled
})
describe('block number', () => {
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).should.eventually.bignumber.equal(blockNumber)
})
})
我怀疑你从柴那里得到了副作用。首先尝试不使用它进行测试,例如:
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));
});
});
});
摩卡知道如何处理退回的承诺,所以真的不需要柴。这是不必要的糖。这原来是个愚蠢的打字错误。我输入了
fullfilled
而不是completed
文件存储.store.q的代码是什么?我已经按照承诺广泛使用了柴。它从来没有遇到过问题。以一种有意义的方式在这里添加代码需要做很多工作(这是一个非常复杂的函数)。然而,它明确地返回了一个适当的承诺,这一点可以从1)then函数激发和2)chai作为承诺接受它作为实现什么是.bignumber
?它来自chai bignumber