Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# directly
# dreckly

## Like Promise.all, only less so

Expand All @@ -8,14 +8,14 @@ This module could more descriptively be named Promise.allButNotAllAtOnce. It tak
Now supports throttling of potentially infinite queues of Promises (see notes on the `Queue` class below)

## About the name
In the West Country people will often promise to do things 'directly' `[drekt-lee]`, meaning they'll do it when they're good and ready, possibly never. Example usage:
In the West Country people will often promise to do things 'dreckly' `[drekt-lee]`, meaning they'll do it when they're good and ready, possibly never. Example usage:

> I'll wash the dishes directly, my lover
> I'll wash the dishes dreckly, my lover

## Usage

```js
const directly = require('directly');
const dreckly = require('dreckly');
const urls = []; // a big array of urls
const fetchers = urls.map(function (url) {
return function () {
Expand All @@ -24,7 +24,7 @@ const fetchers = urls.map(function (url) {
});


directly(10, fetchers)
dreckly(10, fetchers)
.then(function (results) {
// handle exactly as if it was a Promise.all()
});
Expand All @@ -34,30 +34,30 @@ directly(10, fetchers)
Can also be called as a constructor (in which case the `.run()` method should be used)

```js
const Directly = require('Directly');
const throttledRequests = new Directly(10, fetchers)
const Dreckly = require('Dreckly');
const throttledRequests = new Dreckly(10, fetchers)

throttledRequests
.run()
.then(function (results) {
// handle exactly as if it was a Promise.all()
})

// can be used to stop the directly instance prematurely
// can be used to stop the dreckly instance prematurely
throttledRequests.terminate()
```

To handle an infinite queue of promises use the `Queue` class to wrap your array of functions

```js
fetchers = new directly.Queue(fetchers);
directly(10, fetchers)
fetchers = new dreckly.Queue(fetchers);
dreckly(10, fetchers)
.catch(function (errorObject) {
// You can handle any errors in here
// The error object has 3 properties
// error: The error thrown
// nextError: A promise which will reject the next time an error is encountered
// terminate: A function to call which will terminate the directly instance
// terminate: A function to call which will terminate the dreckly instance
});

// use push to add to the execution queue. Will work even if the queue has fallen idle
Expand Down
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "directly",
"main": "directly.js",
"name": "dreckly",
"main": "dreckly.js",
"version": "0.0.0",
"homepage": "https://github.com/wheresrhys/directly",
"homepage": "https://github.com/wheresrhys/dreckly",
"authors": [
"Rhys Evans <wheresrhys@gmail.com>"
],
Expand Down
10 changes: 5 additions & 5 deletions directly.js → dreckly.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ function getRemover (arr, target) {
};
}

class Directly {
class Dreckly {
constructor (concurrence, funcs) {
this.results = [];
this.concurrence = concurrence;
this.funcs = funcs;
this.terminates = Array.isArray(this.funcs);
this.cancelled = false;
if (!Array.isArray(this.funcs)) {
this.funcs.attachDirectlyInstance(this);
this.funcs.attachDrecklyInstance(this);
}
this.competitors = [];
}

run () {
if (typeof this.funcs[0] !== 'function') {
throw new TypeError('directly expects a list functions that return a Promise, not a list of Promises')
throw new TypeError('dreckly expects a list functions that return a Promise, not a list of Promises')
}
if (this.terminates) {
if (this.funcs.length <= this.concurrence) {
Expand Down Expand Up @@ -111,8 +111,8 @@ class Directly {
}

module.exports = function SmartConstructor (concurrence, funcs) {
const directly = new Directly(concurrence, funcs)
return (this instanceof SmartConstructor) ? directly : directly.run();
const dreckly = new Dreckly(concurrence, funcs)
return (this instanceof SmartConstructor) ? dreckly : dreckly.run();
};

module.exports.Queue = require('./lib/queue');
6 changes: 3 additions & 3 deletions lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class Queue {
this.items = items || [];
}

attachDirectlyInstance (directly) {
this.directly = directly;
attachDrecklyInstance (dreckly) {
this.dreckly = dreckly;
}

push (func) {
this.items.push.apply(this.items, [].slice.call(arguments));
this.directly.run();
this.dreckly.run();
}

shift () {
Expand Down
Loading