UNPKG

1.83 kBJavaScriptView Raw
1"use strict";
2/*! *****************************************************************************
3Copyright (c) Microsoft Corporation.
4Licensed under the Apache License, Version 2.0.
5
6See LICENSE file in the project root for details.
7***************************************************************************** */
8Object.defineProperty(exports, "__esModule", { value: true });
9/**
10 * Encapsulates a Promise and exposes its resolve and reject callbacks.
11 */
12class 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}
59exports.Deferred = Deferred;
60function identity(value) { return value; }