Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
5138219563 | |||
93f426cce0 | |||
e42061047a | |||
3b07f99488 | |||
3d252a5e3e | |||
d82722296b |
@ -1,6 +1,6 @@
|
||||
# smartpromise
|
||||
# @pushrocks/smartpromise
|
||||
|
||||
dropin replacement for q
|
||||
smart helpers that work with promises
|
||||
|
||||
## Availabililty
|
||||
|
||||
@ -29,7 +29,7 @@ Use TypeScript for best in class instellisense.
|
||||
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
|
||||
|
||||
```javascript
|
||||
import * as q from 'smartq'
|
||||
import * as q from '@pushrocks/smartpromise'
|
||||
|
||||
// Deferred
|
||||
// -----------------------------------------------
|
||||
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"version": "2.0.3",
|
||||
"version": "3.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"private": false,
|
||||
"version": "2.0.3",
|
||||
"version": "3.0.0",
|
||||
"description": "dropin replacement for q",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
|
14
test/test.ts
14
test/test.ts
@ -1,10 +1,6 @@
|
||||
import { expect, tap } from 'tapbundle';
|
||||
import * as q from '../ts/index';
|
||||
|
||||
let myCallback = (someValue1: string, cb?) => {
|
||||
cb(null, someValue1);
|
||||
};
|
||||
|
||||
tap.test('should return a Deferred for .defer()', async () => {
|
||||
let myDeferred = q.defer();
|
||||
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
||||
@ -20,16 +16,10 @@ tap.test('should let types flow through the Promise', async () => {
|
||||
return expectPromise;
|
||||
});
|
||||
|
||||
tap.test('should promisify a callback', async () => {
|
||||
let myPromisified = q.promisify(myCallback);
|
||||
let expectPromise = expect(myPromisified('hi')).to.eventually.equal('hi');
|
||||
return await expectPromise;
|
||||
});
|
||||
|
||||
tap.test('should map callbacks', async () => {
|
||||
let inputArray = ['hi', 'awesome'];
|
||||
let myPromisified = q.promisify(myCallback);
|
||||
let expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
||||
const myPromisified = async (myInput) => { return myInput };
|
||||
const expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(inputArray);
|
||||
return expectPromise;
|
||||
});
|
||||
|
||||
|
11
ts/index.ts
11
ts/index.ts
@ -1,5 +1,3 @@
|
||||
import * as util from 'util';
|
||||
|
||||
export interface IResolve<T> {
|
||||
(value?: T | Promise<T>): void;
|
||||
}
|
||||
@ -42,14 +40,15 @@ export let rejectedPromise = err => {
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
export let promisify = util.promisify;
|
||||
interface IAsyncFunction<T> {
|
||||
(someArg: T):Promise<T>
|
||||
}
|
||||
|
||||
export let map = async <T>(inputArg: T[], functionArg) => {
|
||||
let promisifedFunction = promisify(functionArg);
|
||||
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T> ) => {
|
||||
let promiseArray: Promise<any>[] = [];
|
||||
let resultArray = [];
|
||||
for (let item of inputArg) {
|
||||
let promise: Promise<any> = promisifedFunction(item);
|
||||
let promise: Promise<any> = functionArg(item);
|
||||
promiseArray.push(promise);
|
||||
promise.then(x => {
|
||||
resultArray.push(x);
|
||||
|
Reference in New Issue
Block a user