UNPKG

prex

Version:

Async coordination primitives and extensions on top of ES6 Promises

61 lines (59 loc) 1.83 kB
"use strict"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Licensed under the Apache License, Version 2.0. See LICENSE file in the project root for details. ***************************************************************************** */ Object.defineProperty(exports, "__esModule", { value: true }); /** * Encapsulates a Promise and exposes its resolve and reject callbacks. */ class Deferred { /** * Initializes a new instance of the Deferred class. */ constructor() { this._promise = new Promise((resolve, reject) => { this._resolve = resolve; this._reject = reject; }); } /** * Gets the promise. */ get promise() { return this._promise; } /** * Gets the callback used to resolve the promise. */ get resolve() { return this._resolve; } /** * Gets the callback used to reject the promise. */ get reject() { return this._reject; } /** * Gets a NodeJS-style callback that can be used to resolve or reject the promise. */ get callback() { if (!this._callback) { this._callback = this.createCallback(identity); } return this._callback; } /** * Creates a NodeJS-style callback that can be used to resolve or reject the promise with multiple values. */ createCallback(selector) { return (err, ...args) => { if (err !== null && err !== undefined) { this._reject(err); } else { this._resolve(selector(...args)); } }; } } exports.Deferred = Deferred; function identity(value) { return value; }