2.1.9 • Published 5 years ago

promised-deferred v2.1.9

Weekly downloads
6
License
ISC
Repository
github
Last release
5 years ago

promised-deferred

Joins @Medikoo's Deffered Library with something the default Promise library from NodeJS can read and interact with

Install

Get the library in your project by doing npm install promised-deferred

Usage

The point of Promised Deferred is that you are able to create regular Node.JS promises, as well as use the functionality of Medikoo's Deferred library, being able to resolve a promise from outside a method or function. An example of an outside resolve (possible through just using the Deferred library):

const PromisedDeferred = require('promised-deferred');

var pd = new PromisedDeferred();

function DoSomething() {
    // Your code here
    return pd.Promise();
}

function DoSomethingElseAfterDoSomething() {
    // Your code here
}

var do = DoSomething();

do.then(() => {
  DoSomethingElseAfterDoSomething();
});

An example of an inside resolve (possible through the default Promise library):

const PromisedDeferred = require('promised-deferred');

var pd = new PromisedDeferred();

DoSomeAsyncCall() {
   AsyncCall((result) =>  {
      pd.Resolve(result);
   }
   return pd.Promise()
}

var do = DoAsyncCall();

do.then((value) => {
    // your code here
});

As stated above, you can still use Promised Deferred as you would a normal Promise() or Deferred(). However, Promise() comes with a nifty thing called .all() which can be beyond useful if your code has to wait until everything is done before continuing, regardless on whether or not it's a normal Async Call like .get() or if it's a custom code that may or may not have a delay.

This is how you do this in Promised Deferred, giving you the best of both worlds: Script1.js:

const PromisedDeferred = require('promised-deferred');
var pd = new PromisedDeferred();

function DoSomething(value) {
    // Your code here
    return pd.Promise();
}

Script2.js:

const PromisedDeferred = require('promised-deferred');

var pd = new PromisedDeferred();

DoSomeAsyncCall() {
   AsyncCall((result) =>  {
      pd.Resolve(result);
   }
   return pd.Promise()
}

index.js:

var promises = [];

promises.push(DoSomething());
promises.push(DoSomeAsyncCall());

Promise.all(promises).then((values) => {
    values.forEach((value, index) => {
        console.log(`${index}: ${value}`);
    }
}

Alternative Use: medikoo's Deferred lib

you can still choose to use the default medikoo's library with all it's functionality:

const PromisedDeferred = require('promised-deferred');
const Deffered = PromisedDeferred.Deferred();

var deferred = new Deferred();
2.1.9

5 years ago

2.1.0

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago