sync_iterator.js
'use strict';
let range = {
from: 1,
to: 5,
[Symbol.iterator]() {
return {
current: this.from,
last: this.to,
next() {
if (this.current <= this.last)
return { done: false, value: this.current++ };
else
return { done: true }
}
}
}
}
for (const value of range) console.log(value);
test.js
'use strict';
const assert = require('assert');
const child_process = require('child_process');
describe('Async iterator', () => {
it('should output sequence', done => {
child_process.exec('node ./async_iterator.js', (error, stdout) => {
assert.equal(stdout, '1\n2\n3\n4\n5\n');
done();
});
});
});