diff --git a/src/wallet.js b/src/wallet.js index ef1405a..a89cd75 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -84,13 +84,14 @@ var Wallet = function (seed, options) { } this.setUnspentOutputsAsync = function(utxo, callback) { + var error = null try { this.setUnspentOutputs(utxo) } catch(err) { - return callback(err) + error = err + } finally { + process.nextTick(function(){ callback(error) }) } - - return callback() } function outputToUnspentOutput(output){ @@ -206,14 +207,15 @@ var Wallet = function (seed, options) { fixedFee = undefined } var tx = null + var error = null try { tx = this.createTx(to, value, fixedFee) } catch(err) { - return callback(err) + error = err + } finally { + process.nextTick(function(){ callback(error, tx) }) } - - callback(null, tx) } this.dustThreshold = 5430 diff --git a/test/wallet.js b/test/wallet.js index f19bc3b..fb5ba1e 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -284,24 +284,25 @@ describe('Wallet', function() { wallet.setUnspentOutputs.restore() }) - it('calls setUnspentOutputs', function(){ + it('calls setUnspentOutputs', function(done){ sinon.stub(wallet, "setUnspentOutputs") - var callback = sinon.spy() - var tx = wallet.setUnspentOutputsAsync(utxo, callback) + var callback = function(){ + assert(wallet.setUnspentOutputs.calledWith(utxo)) + done() + } - assert(wallet.setUnspentOutputs.calledWith(utxo)) - assert(callback.called) + wallet.setUnspentOutputsAsync(utxo, callback) }) - it('when setUnspentOutputs throws an error, it invokes callback with error', function(){ + it('when setUnspentOutputs throws an error, it invokes callback with error', function(done){ sinon.stub(wallet, "setUnspentOutputs").throws() - var callback = sinon.spy() - var tx = wallet.setUnspentOutputsAsync(utxo, callback) - - assert(callback.called) - assert(callback.args[0][0] instanceof Error) + var callback = function(err){ + assert(err instanceof Error) + done() + } + wallet.setUnspentOutputsAsync(utxo, callback) }) }) }) @@ -549,34 +550,41 @@ describe('Wallet', function() { wallet.createTx.restore() }) - it('calls createTx', function(){ + it('calls createTx', function(done){ sinon.stub(wallet, "createTx").returns("fakeTx") - var callback = sinon.spy() - var tx = wallet.createTxAsync(to, value, callback) + var callback = function(err, tx){ + assert(wallet.createTx.calledWith(to, value)) + assert.equal(err, null) + assert.equal(tx, "fakeTx") + done() + } - assert(wallet.createTx.calledWith(to, value)) - assert(callback.calledWith(null, "fakeTx")) + wallet.createTxAsync(to, value, callback) }) - it('calls createTx correctly when fee is specified', function(){ + it('calls createTx correctly when fee is specified', function(done){ sinon.stub(wallet, "createTx").returns("fakeTx") - var callback = sinon.spy() - var tx = wallet.createTxAsync(to, value, fee, callback) + var callback = function(err, tx){ + assert(wallet.createTx.calledWith(to, value, fee)) + assert.equal(err, null) + assert.equal(tx, "fakeTx") + done() + } - assert(wallet.createTx.calledWith(to, value, fee)) - assert(callback.calledWith(null, "fakeTx")) + wallet.createTxAsync(to, value, fee, callback) }) - it('when createTx throws an error, it invokes callback with error', function(){ + it('when createTx throws an error, it invokes callback with error', function(done){ sinon.stub(wallet, "createTx").throws() - var callback = sinon.spy() - var tx = wallet.createTxAsync(to, value, callback) + var callback = function(err, tx){ + assert(err instanceof Error) + done() + } - assert(callback.called) - assert(callback.args[0][0] instanceof Error) + wallet.createTxAsync(to, value, callback) }) })