All Projects → jeffreylanters → unity-promises

jeffreylanters / unity-promises

Licence: MIT license
Promises provide a simpler alternative for executing, composing, and managing asynchronous operations when compared to traditional callback-based approaches. They also allow you to handle asynchronous errors using approaches that are similar to synchronous try/catch.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to unity-promises

Popsicle
Simple HTTP requests for node and the browser
Stars: ✭ 238 (+561.11%)
Mutual labels:  promise
promise4j
Fluent promise framework for Java
Stars: ✭ 20 (-44.44%)
Mutual labels:  promise
promise-waterfall
promise-waterfall extends promise utilities with sequential flow control function like async.waterfall
Stars: ✭ 28 (-22.22%)
Mutual labels:  promise
Spirit
Modern modular library for building web applications
Stars: ✭ 241 (+569.44%)
Mutual labels:  promise
Axios
Promise based HTTP client for the browser and node.js
Stars: ✭ 89,857 (+249502.78%)
Mutual labels:  promise
promised-hooks
Middleware utility for your Promises
Stars: ✭ 25 (-30.56%)
Mutual labels:  promise
Massive Js
A data mapper for Node.js and PostgreSQL.
Stars: ✭ 2,521 (+6902.78%)
Mutual labels:  promise
promise-yes
Slide about how to use Promise in JavaScript.
Stars: ✭ 13 (-63.89%)
Mutual labels:  promise
fetchye
✨ If you know how to use Fetch, you know how to use Fetchye [fetch-yae]. Simple React Hooks, Centralized Cache, Infinitely Extensible.
Stars: ✭ 36 (+0%)
Mutual labels:  promise
parley
Flow control harness for implementors. Builds a Deferred object that supports async/await, promise chaining, and conventional Node callbacks.
Stars: ✭ 23 (-36.11%)
Mutual labels:  promise
Task Easy
A simple, customizable, and lightweight priority queue for promises.
Stars: ✭ 244 (+577.78%)
Mutual labels:  promise
When
⏰ A lightweight implementation of Promises in Swift
Stars: ✭ 250 (+594.44%)
Mutual labels:  promise
promiviz
Visualize JavaScript Promises on the browser. Visualize the JavaScript Promise APIs and learn. It is a playground to learn about promises faster, ever!
Stars: ✭ 79 (+119.44%)
Mutual labels:  promise
Await Of
await wrapper for easier errors handling without try-catch
Stars: ✭ 240 (+566.67%)
Mutual labels:  promise
market-pricing
Wrapper for the unofficial Steam Market Pricing API
Stars: ✭ 21 (-41.67%)
Mutual labels:  promise
Api
Promise and RxJS APIs around Polkadot and any Substrate-based chain RPC calls. It is dynamically generated based on what the Substrate runtime provides in terms of metadata. Full documentation & examples available
Stars: ✭ 232 (+544.44%)
Mutual labels:  promise
promise-all-properties
A helper function that recevies an object with a promise in each property and returns a promise that resolves to an object with the same properties and the resolved values of the promises
Stars: ✭ 26 (-27.78%)
Mutual labels:  promise
vue-geolocation
ask to your users their coordinates, and wrap them into a Promise
Stars: ✭ 88 (+144.44%)
Mutual labels:  promise
alls
Just another library with the sole purpose of waiting till all promises to complete. Nothing more, Nothing less.
Stars: ✭ 13 (-63.89%)
Mutual labels:  promise
bluff
🙏 Promise A+ implementation
Stars: ✭ 14 (-61.11%)
Mutual labels:  promise

readme splash

license openupm build deployment stars awesome size sponsors donate

Promises provide a simpler alternative for executing, composing, and managing asynchronous operations when compared to traditional callback-based approaches. They also allow you to handle asynchronous errors using approaches that are similar to synchronous try/catch.

Installation · Documentation · License

Made with by Jeffrey Lanters

Installation

Using the Unity Package Manager

Install the latest stable release using the Unity Package Manager by adding the following line to your manifest.json file located within your project's Packages directory, or by adding the Git URL to the Package Manager Window inside of Unity.

"nl.elraccoone.promises": "git+https://github.com/jeffreylanters/unity-promises"

Using OpenUPM

The module is availble on the OpenUPM package registry, you can install the latest stable release using the OpenUPM Package manager's Command Line Tool using the following command.

openupm add nl.elraccoone.promises

Documentation

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

A Promise is in one of these states:

  • pending: initial state, neither fulfilled nor rejected.
  • fulfilled: meaning that the operation completed successfully.
  • rejected: meaning that the operation failed.

A pending promise can either be fulfilled with a value, or rejected with a reason (error). When either of these options happens, the associated handlers queued up by a promise's then method are called. (If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached.)

Syntax

A function that is passed with the arguments resolve and reject. The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object). The resolve and reject functions, when called, resolve or reject the promise, respectively. The executor normally initiates some asynchronous work, and then, once that completes, either calls the resolve function to resolve the promise or else rejects it if an error occurred. If an error is thrown in the executor function, the promise is rejected. The return value of the executor is ignored.

new Promise ((resolve, reject) => { /* ... */ });
new Promise<ResolveType> ((resolve, reject) => { /* ... */ });
new Promise<ResolveType, RejectType> ((resolve, reject) => { /* ... */ });

Creating a Promise

A Promise object is created using the new keyword and its constructor. This constructor takes as its argument a function, called the "executor function". This function should take two functions as parameters. The first of these functions (resolve) is called when the asynchronous task completes successfully and returns the results of the task as a value. The second (reject) is called when the task fails, and returns the reason for failure, which is typically an error object.

public Promise LoadSomeData () {
  // Return a new void promise and assign the executor.
  return new Promise ((resolve, reject) => {
    // do something asynchronous which eventually calls either:
    resolve (); // to fulfill
    reject (new Exception ("Failed")); // or reject
  });
}

public Promise<int> LoadSomeData () {
  // Return a new promise with a generic resolve type and assign the executor.
  return new Promise<int> ((resolve, reject) => {
    // do something asynchronous which eventually calls either:
    resolve (100); // to fulfill
    reject (new Exception ("Not found!")); // or reject
  });
}

public Promise<string, CustomException> LoadSomeData () {
  // Return a new promise with a generic resolve and reject type and assign the executor.
  return new Promise<string, CustomException> ((resolve, reject) => {
    // do something asynchronous which eventually calls either:
    resolve ("Hello World!"); // to fulfill
    reject (new CustomException ()); // or reject
  });
}

Using a Promise

Using classic Then/Catch callbacks

Execute your function and return a promise. You can use the Promise.Then() and Promise.Catch() methods to set actions that should be triggered when the promise resolves or failes. These methods return promises so they can be chained.

public void Start () {
  this.LoadSomeData()
    .Then (value => { /* ... */ })
    .Catch (reason => { /* ... */ })
    .Finally (() => { /* ... */ });
}

Using Async/Await methods

When chaining multiple promises after one another or when simply wanting to prevent a lot of nested callbacks, use the async await logic to keep things simple. There are two ways of doing so, the simplest way does not require any changes on the promise's creation side. A basic implementation could be as following.

public async void Start () {
  try {
    var value = await this.LoadSomeData ().Async ();
    // "Then"
  } catch (Exception exception) {
    // "Catch"
  }
  // "Finally"
}

Another way would be to wrap the Async before returning it as a promise, this way the promise can be awaited right away when invoking it. But this also means it can no longer be used using callbacks. A basic implementation could be as following.

public async Task<string> LoadSomeData () {
  // Return and await a new Task with a generic type.
  return await new Promise<string> ((resolve, reject) => {
    // do something asynchronous which eventually calls either:
    resolve ("Hello World!"); // to fulfill
    reject (new Exception ("Whoops")); // or reject
  }).Async();
}

public async void Start () {
  var value = await this.LoadSomeData ();
  // ...
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].