1.0.4 • Published 3 years ago

promise-container v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

promise-container

Install

npm install [--save] promise-container
# by yarn
yarn add promise-container

Example

query running promises

import {PromiseContainerManager} from 'promise-container';
const manager = new PromiseContainerManager();
// do something with manager.
const promiseInfos = manager.getAll();
let name: string | undefined;
const namedPromiseInfos = manager.getAllByName(name);
manager.forEach((promiseInfo) => {
  // do something with promiseInfo.
});

manager.forEachByName(name, (namedPromiseInfo) => {
  // do something with promiseInfo.
});

decorate method

const manager = new PromiseContainerManager();
class SomeClass {
  constructor(public context: number) {}

  @ManageAsync(manager, 'scrape task')
  async managedAsync (taskArg1: number, taskArg2: number) {
    // do something with taskArg1, taskArg2.
    expect(this.context).toEqual(100);
    expect(taskArg1).toEqual(1);
    expect(taskArg2).toEqual(2);
    return 3;
  }
}
expect(await new SomeClass(100).managedAsync(1, 2)).toEqual(3);

create instance promise

import {PromiseContainerManager, PromiseM} from 'promise-container';
const manager = new PromiseContainerManager();
const promise = PromiseM(manager, (resolve, reject) => {
  // do something with resolve, reject.
})
await promise;

create async promise

import {PromiseContainerManager, PromiseA} from 'promise-container';
const manager = new PromiseContainerManager();
const fac = PromiseA(manager, 'scrape task', async (taskArg1: A, taskArg2: B) => {
  // do something with taskArg1, taskArg2.
  return 1;
});
expect(await fac(a1, a2)).toEqual(1);

class WrapClass {
  context = 100;

  method: PromiseA(manager, 'scrape task', async (taskArg1: A, taskArg2: B) => {
  // do something with taskArg1, taskArg2.
  expect(this.context).toEqual(100);
  return 1;
}),
}
expect(await new WrapClass.method(a1, a2))).toEqual(1);

create factory promise container

import {PromiseContainerManager, PromiseFacContainer} from 'promise-container';
const manager = new PromiseContainerManager();
const fac = new PromiseFacContainer('scrape task', async (taskArg1: A, taskArg2: B) => {
  // do something with taskArg1, taskArg2.
});
await manager.create(fac, [a1, a2]);
1.0.3-4

3 years ago

1.0.4

3 years ago

1.0.3-2

3 years ago

1.0.3-3

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago