Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

extends-promise

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extends-promise - npm Package Compare versions

Comparing version 1.1.2 to 1.2.0

49

index.js

@@ -40,14 +40,29 @@ "use strict";

map(fn) {
return this.then(res => P.all(res.map(fn)));
}
map(fn, opts) {
const concurrency = opts && opts.concurrency;
filter(fn) {
if (typeof concurrency !== "number" || !isFinite(concurrency) || concurrency < 1) {
return this.then(res => P.all(res.map(fn)));
}
return this.then(res => {
return P
.all(res.map(fn))
.then(filters => res.filter((_, i) => filters[i]));
const ds = res.map(P.defer);
let i = concurrency;
ds.slice(0, concurrency).forEach(d => d.resolve());
return P.all(res.map((x, j) => {
return ds[j].promise
.then(() => fn(x, j, res))
.tap(() => ds[i] && ds[i++].resolve());
}));
});
}
filter(fn, opts) {
return this
.map((x, i, a) => P.all([fn(x, i, a), x]), opts)
.then(res => res.filter(t => t[0]).map(t => t[1]));
}
reduce(fn, initialValue) {

@@ -67,2 +82,14 @@ const reducer = (y, x, i, a) => P.resolve(y).then(z => fn(z, x, i, a));

// Methods should also be available as static
static defer() {
let resolve = null;
let reject = null;
const promise = new P((res, rej) => {
resolve = res;
reject = rej;
});
return { resolve, reject, promise };
}
static delay(ms, val) {

@@ -72,8 +99,8 @@ return P.resolve(val).delay(ms);

static map(val, fn) {
return P.resolve(val).map(fn);
static map(val, fn, opts) {
return P.resolve(val).map(fn, opts);
}
static filter(val, fn) {
return P.resolve(val).filter(fn);
static filter(val, fn, opts) {
return P.resolve(val).filter(fn, opts);
}

@@ -80,0 +107,0 @@

{
"name": "extends-promise",
"version": "1.1.2",
"version": "1.2.0",
"description": "Micro-library that extends native promises",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -43,2 +43,27 @@ [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url]

#### `defer(Function fn)`
Creates a `promise` along with distinct `resolve` and `reject` methods. This is handy
if you need a promise placeholder or need to promisify something non-standard. In most
cases you are better off with `new P((resolve, reject) => {})` or `P.promisify`. Beware of
[the deferred anti-pattern](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern).
```js
const P = require("extends-promise");
const deferred = P.defer();
deferred.promise.then(console.log);
deferred.resolve("Hello, World!");
```
```js
const P = require("extends-promise");
const deferred = P.defer();
deferred.promise.catch(console.error);
deferred.reject(new Error("Goodbye, World!"));
```
#### `extend(Promise)`

@@ -120,5 +145,7 @@

#### `map(Function method)`
#### `map(Function method[, options])`
Similar to `[].map` but, waits for promises returned from the mapping function to resolve. NOTE: This will run all map functions concurrently.
Similar to `[].map` but, waits for promises returned from the mapping function to resolve. By
default all methods will be run concurrently. You may also pass a concurrency option,
`{ concurrency : 1 }`.

@@ -134,6 +161,19 @@ ```js

#### `filter(Function method)`
```js
const P = require("extends-promise");
Similar to `[].filter` but waits for promises returned from the filtering function to resolve.
P.resolve([1, 2, 3])
.map(res => P.delay(Math.random() * 100, res), {
concurrency : 3
})
// [1, 2, 3]
.then(console.log);
```
#### `filter(Function method[, options])`
Similar to `[].filter` but waits for promises returned from the filtering function to resolve. By
default all methods will be run concurrently. You may also pass a concurrency option,
`{ concurrency : 1 }`.
```js

@@ -148,2 +188,13 @@ const P = require("extends-promise");

```js
const P = require("extends-promise");
P.resolve([1, 2, 3, 4])
.filter(res => P.delay(Math.random() * 100, res % 2), {
concurrency : 3
})
// [1, 3]
.then(console.log);
```
#### `reduce(Function method[, initialValue])`

@@ -150,0 +201,0 @@

@@ -31,2 +31,18 @@ "use strict";

});
it("should accept concurrency bound", () => {
const concurrency = 3;
let running = 0;
return P
.map(Array(10).fill(0), () => {
running += 1;
assert(running <= concurrency, "Too much concurrency!");
return P
.delay(5)
.then(() => running -= 1);
}, { concurrency });
});
});

@@ -54,2 +70,20 @@

});
it("should accept concurrency bound", () => {
const concurrency = 3;
let running = 0;
return P
.filter([1, 2, 3, 4], x => {
running += 1;
assert(running <= concurrency, "Too much concurrency!");
return P
.delay(5)
.then(() => running -= 1)
.return(x % 2);
}, { concurrency })
.then(res => assert.deepEqual(res, [1, 3]));
});
});

@@ -56,0 +90,0 @@

@@ -11,2 +11,23 @@ "use strict";

describe("Helpers", () => {
describe(".defer", () => {
it("should be able to resolve a deferred promise", () => {
const d = P.defer();
d.resolve(1);
return d.promise.then(res => assert.strictEqual(res, 1));
});
it("should be able to reject a deferred promise", () => {
const d = P.defer();
const err = new Error();
d.reject(err);
return d.promise
.then(() => P.reject(new Error()))
.catch(res => assert.strictEqual(res, err));
});
});
describe(".return", () => {

@@ -13,0 +34,0 @@ it("should be able to return value", () => {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc