-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.test.js
More file actions
24 lines (20 loc) · 808 Bytes
/
ex2.test.js
File metadata and controls
24 lines (20 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'use strict';
const { sequence } = require('./ex2');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
const order = [];
const tasks = [
() => Promise.resolve().then(() => { order.push('a'); return 'A'; }),
() => new Promise((resolve) => setTimeout(() => { order.push('b'); resolve('B'); }, 5)),
() => Promise.reject(new Error('fail')),
() => Promise.resolve().then(() => { order.push('c'); return 'C'; })
];
sequence(tasks).then(() => {
assert(false, 'sequence should reject');
}).catch((err) => {
assert(err instanceof Error, 'rejection should be Error');
assert(err.message === 'fail', 'error message should match');
assert(order.join('') === 'ab', 'tasks after rejection should not run');
console.log('ex2 tests passed');
});