Socket
Socket
Sign inDemoInstall

synchronous-promise

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

synchronous-promise - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

6

index.js

@@ -68,4 +68,4 @@ 'use strict';

}
var next = this._next.shift();
try {
var next = this._next.shift();
if (!next) {

@@ -80,3 +80,3 @@ return;

self._applyNext();
}).catch(function(e) {
}).catch(function (e) {
throw e;

@@ -92,3 +92,3 @@ });

if (next[1]) {
try { next[1](e); } catch (e) { console.log(e); }
try { next[1](e); } catch (ignore) { }
} else {

@@ -95,0 +95,0 @@ this._catchData = [e];

{
"name": "synchronous-promise",
"version": "1.0.2",
"version": "1.0.3",
"description": "Synchronous Promise-like prototype to use in testing where you would have used an ES6 Promise",

@@ -15,3 +15,4 @@ "main": "index.js",

"pretest": "run-s lint",
"test": "mocha index.spec.js"
"test": "mocha index.spec.js",
"prepublish": "run-s test"
},

@@ -26,4 +27,5 @@ "author": "Davyd McColl <davydm@gmail.com> (https://github.com/fluffynuts)",

"nodemon": "^1.10.0",
"npm-run-all": "^2.3.0",
"run-sequence": "^1.2.2"
}
}

@@ -17,15 +17,17 @@ # synchronous-promise

describe('the thing', () => {
it('will do some stuff', () => {
// Arrange
const asyncLibraryFake = {
someMethod: sinon.stub().returns('happy value!')
},
sut = createSystemUnderTestWith(asyncLibraryFake);
// Act
sut.doSomethingInteresting();
// Assert
// [*]
})
});
```javascript
describe('the thing', () => {
it('will do some stuff', () => {
// Arrange
const asyncLibraryFake = {
someMethod: sinon.stub().returns('happy value!')
},
sut = createSystemUnderTestWith(asyncLibraryFake);
// Act
sut.doSomethingInteresting();
// Assert
// [*]
})
});
```

@@ -42,17 +44,18 @@ [*] Ideally, we'd just have assertions here, but the code above has backgrounded,

describe('the thing', () => {
it('will do some stuff', done => {
// Arrange
const asyncLibraryFake = {
someMethod: sinon.stub().returns('happy value!')
},
sut = createSystemUnderTestWith(asyncLibraryFake);
// Act
sut.doSomethingInteresting().then(() => {
// Assert
done()
});
})
```javascript
describe('the thing', () => {
it('will do some stuff', done => {
// Arrange
const asyncLibraryFake = {
someMethod: sinon.stub().returns('happy value!')
},
sut = createSystemUnderTestWith(asyncLibraryFake);
// Act
sut.doSomethingInteresting().then(() => {
// Assert
done()
});
})
});
```
***And there's nothing with this strategy.***

@@ -78,38 +81,47 @@

##Usage
### Usage
SynchronousPromise looks (from the outside) a lot like an ES6 promise. We construct
the same:
var promise = new SynchronousPromise((resolve, reject) => {
if (Math.random() < 0.1) {
reject('unlucky!');
} else {
resolve('lucky!');
}
});
```javascript
var promise = new SynchronousPromise((resolve, reject) => {
if (Math.random() < 0.1) {
reject('unlucky!');
} else {
resolve('lucky!');
}
});
```
They can, of course, be chained:
var initial - new SynchronousPromise((resolve, reject) => {
resolve('happy!');
});
initial.then(message => {
console.log(message);
})
```javascript
var initial = new SynchronousPromise((resolve, reject) => {
resolve('happy!');
});
initial.then(message => {
console.log(message);
})
```
And have error handling, either from the basic A+ spec:
initial.then(message => {
console.log(message);
}, error => {
console.error(error);
});
```javascript
initial.then(message => {
console.log(message);
}, error => {
console.error(error);
});
```
Or using the more familiar `catch()`:
initial.then(message => {
console.log(message);
}).catch(error => {
console.error(error);
})
```javascript
initial.then(message => {
console.log(message);
}).catch(error => {
console.error(error);
})
```

@@ -120,18 +132,19 @@ `.catch()` starts a new promise chain, so you can pick up with new logic

##Statics
### Statics
`.all()`, `.resolve()` and `.reject()` are available on the `SynchronousPromise`
object itself:
SynchronousPromise.all([p1, p2]).then(results => {
// results is an array of results from all promises
}).catch(err => {
// err is any single error thrown by a promise in the array
});
```javascript
SynchronousPromise.all([p1, p2]).then(results => {
// results is an array of results from all promises
}).catch(err => {
// err is any single error thrown by a promise in the array
});
SynchronousPromise.resolve('foo'); // creates an already-resolved promise
SynchronousPromise.resolve('foo'); // creates an already-resolved promise
SynchronousPromise.reject('bar'); // creats an already-rejected promise
SynchronousPromise.reject('bar'); // creats an already-rejected promise
```
##Extras
### Extras
`SynchronousPromise` also provides two extra functions to make testing a little

@@ -142,23 +155,43 @@ easier:

SynchronousPromise.resolve('abc').then(data => {
// this will be run
return '123';
}).pause().then(data2 => {
// we don't get here without resuming
});
```javascript
SynchronousPromise.resolve('abc').then(data => {
// this will be run
return '123';
}).pause().then(data2 => {
// we don't get here without resuming
});
```
and `resume()` resumes operations:
var
promise = SynchronousPromise.resolve('123').pause(),
captured = null;
promise.then(data => {
captured = data;
});
```javascript
var
promise = SynchronousPromise.resolve('123').pause(),
captured = null;
promise.then(data => {
captured = data;
});
expect(data).to.be.null; // because we paused...
promise.resume();
expect(data).to.equal('123'); // because we resumed...
expect(data).to.be.null; // because we paused...
promise.resume();
expect(data).to.equal('123'); // because we resumed...
```
You can use `pause()` and `resume()` to test the state of your system under
test at defined points in a series of promise chains
test at defined points in a series of promise chains
### ES5
SynchronousPromise is purposefully written with prototypical, ES5 syntax so you
can use it from ES5 if you like.
### Production code
The main aim of SynchronousPromise is to facilitate easier testing. That being
said, it appears to conform to expected `Promise` behaviour, barring the
always-backgrounded behaviour. One might be tempted to just use it everywhere.
**However**: I'd highly recommend using *any* of the more venerable promise implementations
instead of SynchronousPromise in your production code -- preferably the vanilla
ES6 Promise, where possible (or the shim, where you're in ES5). Or Q.
Or jQUery.Deferred(). Basically, this seems to work quite well for testing and
I've tried to implement every behaviour I'd expect from a promise -- but I'm
pretty sure that a native `Promise` will be better for production code any day.
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