Skip to content

Commit

Permalink
Catch errors in forAwaitEach (#28)
Browse files Browse the repository at this point in the history
* add tests for exceptions and catch them

* make sure tests fail if there are unhandledRejections
  • Loading branch information
NeoPhi authored and leebyron committed Sep 28, 2017
1 parent 325e8ff commit e7769da
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 10 deletions.
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,17 +641,20 @@ function forAwaitEach(source, callback, thisArg) {
var asyncIterator = createAsyncIterator(source)
if (asyncIterator) {
var i = 0
return new Promise(function(resolve) {
return new Promise(function(resolve, reject) {
function next() {
return asyncIterator.next().then(function(step) {
if (!step.done) {
Promise.resolve(
callback.call(thisArg, step.value, i++, source)
).then(next)
} else {
resolve()
}
})
return asyncIterator
.next()
.then(function(step) {
if (!step.done) {
Promise.resolve(callback.call(thisArg, step.value, i++, source))
.then(next)
.catch(reject)
} else {
resolve()
}
})
.catch(reject)
}
next()
})
Expand Down
96 changes: 96 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

var assert = require('assert')

process.on('unhandledRejection', error => {
console.log('\x1B[31m \u2718 \x1B[0m unhandledRejection')
process.exitCode = 1
process.on('beforeExit', function() {
console.error('\n\x1B[41m unhandledRejection \x1B[0m')
console.error((error && error.stack) || error)
})
})

async function test(name, rule) {
try {
var result = await rule()
Expand Down Expand Up @@ -1135,3 +1144,90 @@ test('forAwaitEach iterates over custom AsyncIterable', () => {
])
)
})

test('forAwaitEach catches callback errors', () => {
var myIterable = iterSampleFib()
var error = new Error()
var callback = () => {
throw error
}
return forAwaitEach(myIterable, callback).catch(e =>
assert.strictEqual(e, error)
)
})

function* genError(error, on) /*: Generator<number, void, void> */ {
var x = 0
while (true) {
if (x >= on) {
throw error
}
yield x
x = x + 1
}
}

test('forAwaitEach catches Iterable errors', async () => {
await Promise.all(
[0, 1, 2].map(async on => {
var spy = createSpy()
var error = new Error()
var myIterable = genError(error, on)
await forAwaitEach(myIterable, spy, spy).catch(e =>
assert.strictEqual(e, error)
)
assert.equal(spy.calls.length, on)
})
)
})

function ChirpError(error, on, inPromise) {
this.error = error
this.on = on
this.inPromise = inPromise
}

ChirpError.prototype[$$asyncIterator] = function() {
return {
error: this.error,
on: this.on,
inPromise: this.inPromise,
num: 0,
next() {
if (!this.inPromise && this.num >= this.on) {
throw this.error
}
return new Promise(resolve => {
if (this.num >= this.on) {
throw this.error
} else {
resolve({ value: this.num++, done: false })
}
})
},
[$$asyncIterator]() {
return this
}
}
}

test('forAwaitEach catches AsyncIterable errors', async () => {
await Promise.all(
[
{ on: 0, inPromise: false },
{ on: 0, inPromise: true },
{ on: 1, inPromise: false },
{ on: 1, inPromise: true },
{ on: 2, inPromise: false },
{ on: 2, inPromise: true }
].map(async ({ on, inPromise }) => {
var spy = createSpy()
var error = new Error()
var myAsyncIterable = new ChirpError(error, on, inPromise)
await forAwaitEach(myAsyncIterable, spy, spy).catch(e =>
assert.strictEqual(e, error)
)
assert.equal(spy.calls.length, on)
})
)
})

0 comments on commit e7769da

Please sign in to comment.