Giter Site home page Giter Site logo

external-promise's Introduction

External Promise

A promise that can be resolved or rejected externally.

Installation

npm install external-promise --save

Basic Usage

// Initializing the promise
const p = new ExternalPromise();

// Resolving the promise after a timeout
setTimeout(() => { p.resolve(); }, 1000);

// Awaiting the promise
await p;

Documentation

Practical Example

Example showing 2 API methods. The first method initializes the promise and awaits its resolution. The second method resolves it.

class Item {
  private name: string;

  private static counter = 1;

  private isBeingRenamed = false;

  private renamePromise: ExternalPromise<string> | null = null;

  public costructor() {
    this.name = `Item ${Item.counter}`;
    Item.counter += 1;
  }

  // This is a simple example and reactions of UI to the state of the instance are not considered.
  // This could easily be a MobX observable where the UI observes isBeingRenamed and name.

  /**
   * An API method that initializes the UI rename process and awaits the operation to complete.
   */
  public async rename(): Promise<string | false> {
    // Cancel previous rename if pending
    if (this.renamePromise && this.renamePromise.getState() === 'pending') {
      this.renamePromise.resolve(false);
    }

    this.isBeingRenamed = true;

    // Initializing the promise
    this.renamePromise = new ExternalPromise();

    // Awaiting the promise
    const result = await this.renamePromise;

    this.isBeingRenamed = false;

    if (result !== false) {
      this.name = result;
    }

    return result;
  }

  /**
   * A method that finalizes or cancels the rename that can be called from another part of the UI
   * when the user has finished renaming.
   */
  public resolveRename(name: string | false): void {
    if (this.promiseRename_ === null) {
      return;
    }

    if (this.promiseRename_?.getState() === 'pending') {
      // Resolving the promise
      this.promiseRename_.resolve(result);
    }

    this.promiseRename_ = null;
  }
}

external-promise's People

Contributors

mckravchyk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.