|
5 | 5 | longSleep, |
6 | 6 | retry, |
7 | 7 | retryInterval, |
| 8 | + nodeify, |
| 9 | + nodeifyAll, |
8 | 10 | parallel, |
9 | 11 | asyncmap, |
10 | 12 | asyncfilter, |
@@ -173,6 +175,60 @@ describe('retry', function () { |
173 | 175 | }); |
174 | 176 | }); |
175 | 177 |
|
| 178 | +describe('nodeifyAll', function () { |
| 179 | + const asyncFn = async function (val: string): Promise<string> { |
| 180 | + await sleep(15); |
| 181 | + return val; |
| 182 | + }; |
| 183 | + const asyncFn2 = async function (val: string): Promise<string[]> { |
| 184 | + await sleep(15); |
| 185 | + return [val, val + val]; |
| 186 | + }; |
| 187 | + const badAsyncFn = async function (): Promise<never> { |
| 188 | + await sleep(15); |
| 189 | + throw new Error('boo'); |
| 190 | + }; |
| 191 | + const cbMap = nodeifyAll({asyncFn, asyncFn2, badAsyncFn}); |
| 192 | + it('should turn async functions into nodey things', function (done) { |
| 193 | + const start = Date.now(); |
| 194 | + nodeify(asyncFn('foo'), function (err: Error | null, val?: string, val2?: string) { // eslint-disable-line promise/prefer-await-to-callbacks |
| 195 | + expect(err).to.not.exist; |
| 196 | + expect(val2).to.not.exist; |
| 197 | + expect(val!).to.equal('foo'); |
| 198 | + expect(Date.now() - start).to.be.at.least(14); |
| 199 | + done(); |
| 200 | + }); |
| 201 | + }); |
| 202 | + it('should turn async functions into nodey things via nodeifyAll', function (done) { |
| 203 | + const start = Date.now(); |
| 204 | + cbMap.asyncFn('foo', function (err: Error | null, val?: string, val2?: string) { // eslint-disable-line promise/prefer-await-to-callbacks |
| 205 | + expect(err).to.not.exist; |
| 206 | + expect(val2).to.not.exist; |
| 207 | + expect(val!).to.equal('foo'); |
| 208 | + expect(Date.now() - start).to.be.at.least(14); |
| 209 | + done(); |
| 210 | + }); |
| 211 | + }); |
| 212 | + it('should turn async functions into nodey things with mult params', function (done) { |
| 213 | + const start = Date.now(); |
| 214 | + nodeify(asyncFn2('foo'), function (err: Error | null, val?: string[]) { // eslint-disable-line promise/prefer-await-to-callbacks |
| 215 | + expect(err).to.not.exist; |
| 216 | + expect(val!).to.eql(['foo', 'foofoo']); |
| 217 | + expect(Date.now() - start).to.be.at.least(14); |
| 218 | + done(); |
| 219 | + }); |
| 220 | + }); |
| 221 | + it('should handle errors correctly', function (done) { |
| 222 | + const start = Date.now(); |
| 223 | + nodeify(badAsyncFn(), function (err: Error | null, val?: string) { // eslint-disable-line promise/prefer-await-to-callbacks |
| 224 | + expect(val).to.not.exist; |
| 225 | + expect(err!.message).to.equal('boo'); |
| 226 | + expect(Date.now() - start).to.be.at.least(14); |
| 227 | + done(); |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
| 231 | + |
176 | 232 | describe('parallel', function () { |
177 | 233 | const asyncFn = async function (val: number): Promise<number> { |
178 | 234 | await sleep(50); |
|
0 commit comments