1 | ;
|
2 | /*! *****************************************************************************
|
3 | Copyright (c) Microsoft Corporation.
|
4 | Licensed under the Apache License, Version 2.0.
|
5 |
|
6 | See LICENSE file in the project root for details.
|
7 | ***************************************************************************** */
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | /**
|
10 | * Encapsulates a Promise and exposes its resolve and reject callbacks.
|
11 | */
|
12 | class Deferred {
|
13 | /**
|
14 | * Initializes a new instance of the Deferred class.
|
15 | */
|
16 | constructor() {
|
17 | this._promise = new Promise((resolve, reject) => {
|
18 | this._resolve = resolve;
|
19 | this._reject = reject;
|
20 | });
|
21 | }
|
22 | /**
|
23 | * Gets the promise.
|
24 | */
|
25 | get promise() {
|
26 | return this._promise;
|
27 | }
|
28 | /**
|
29 | * Gets the callback used to resolve the promise.
|
30 | */
|
31 | get resolve() { return this._resolve; }
|
32 | /**
|
33 | * Gets the callback used to reject the promise.
|
34 | */
|
35 | get reject() { return this._reject; }
|
36 | /**
|
37 | * Gets a NodeJS-style callback that can be used to resolve or reject the promise.
|
38 | */
|
39 | get callback() {
|
40 | if (!this._callback) {
|
41 | this._callback = this.createCallback(identity);
|
42 | }
|
43 | return this._callback;
|
44 | }
|
45 | /**
|
46 | * Creates a NodeJS-style callback that can be used to resolve or reject the promise with multiple values.
|
47 | */
|
48 | createCallback(selector) {
|
49 | return (err, ...args) => {
|
50 | if (err !== null && err !== undefined) {
|
51 | this._reject(err);
|
52 | }
|
53 | else {
|
54 | this._resolve(selector(...args));
|
55 | }
|
56 | };
|
57 | }
|
58 | }
|
59 | exports.Deferred = Deferred;
|
60 | function identity(value) { return value; }
|