Welcome to mirror list, hosted at ThFree Co, Russian Federation.

waiter.js « lib « libtap « node_modules - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf21ce1eb4ec91ce89971bf0f7c16023b478b245 (plain)
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
class Waiter {
  constructor (promise, cb, expectReject) {
    this.cb = cb
    this.ready = false
    this.value = null
    this.resolved = false
    this.rejected = false
    this.done = false
    this.finishing = false
    this.expectReject = !!expectReject
    this.promise = new Promise(res => this.resolve = res)
    promise.then(value => {
      if (this.done) {
        return
      }

      this.resolved = true
      this.value = value
      this.done = true
      this.finish()
    }).catch(er => this.reject(er))
  }

  reject (er) {
    if (this.done) {
      return
    }

    this.value = er
    this.rejected = true
    this.done = true
    this.finish()
  }

  abort (er) {
    if (this.done) {
      return
    }

    this.ready = true
    this.finishing = false
    this.done = true
    this.value = er
    // make it clear that this is a problem by doing
    // the opposite of what was requested.
    this.rejected = !this.expectReject
    return this.finish()
  }

  finish () {
    if (this.ready && this.done && !this.finishing) {
      this.finishing = true
      this.cb(this)
      this.resolve()
    }
  }
}

module.exports = Waiter;