File: promise.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (59 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* @flow */

import * as promise from '../../src/util/promise.js';

test('promisify', async function(): Promise<void> {
  expect(
    await promise.promisify(function(callback) {
      callback(null, 'foo');
    })(),
  ).toBe('foo');

  expect(
    await promise.promisify(function(data, callback) {
      callback(null, data + 'bar');
    })('foo'),
  ).toBe('foobar');

  expect(
    await promise.promisify(function(callback) {
      callback(null, 'foo', 'bar');
    })(),
  ).toEqual(['foo', 'bar']);

  let error;
  try {
    await promise.promisify(function(callback) {
      callback(new Error('yep'));
    })();
  } catch (e) {
    error = e;
  }
  expect(error && error.message).toEqual('yep');
});

test('queue', async function(): Promise<void> {
  jest.useFakeTimers();
  let running = 0;

  function create(): Promise<void> {
    running++;
    jest.runAllTimers();

    if (running > 5) {
      return Promise.reject(new Error('Concurrency is broken'));
    }

    running--;

    return Promise.resolve();
  }

  await promise.queue([], function() {
    throw new Error("Shouldn't be called");
  });

  await promise.queue(Array(10), create, 5);

  jest.useRealTimers();
});