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

suspend

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

suspend - npm Package Compare versions

Comparing version 0.5.1 to 0.6.0

test/suspend.promise.js

23

lib/suspend.js

@@ -0,1 +1,3 @@

var Promise = require('../node_modules/promise/lib/es6-extensions');
/**

@@ -45,2 +47,23 @@ * Our suspend namespace, which doubles as an alias for `suspend.fn` (although

/**
* Accepts a generator, and returns a new function that returns a promise.
*/
suspend.promise = function promise(generator) {
if (!isGeneratorFunction(generator)) {
throw new Error('First .promise() argument must be a GeneratorFunction.');
}
return function() {
var self = this,
args = Array.prototype.slice.call(arguments);
return new Promise(function(resolve, reject) {
var suspender = new Suspender(generator, function(err, ret) {
err ? reject(err) : resolve(ret);
});
suspender.start(self, args);
});
};
};
/**
* Accepts a generator and an optional callback. The generator is invoked

@@ -47,0 +70,0 @@ * immediately - any errors or returned values are passed to the callback.

14

package.json

@@ -6,3 +6,3 @@ {

"keywords": ["async", "generator", "yield", "callback", "promise", "flow", "control", "suspend"],
"version": "0.5.1",
"version": "0.6.0",
"repository": {

@@ -17,8 +17,10 @@ "type": "git",

},
"dependencies": {
"promise": "^6.0.0"
},
"devDependencies": {
"mocha": "~1.15.1",
"q": "~0.9.7",
"regenerator": "~0.3.1",
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.7.2"
"mocha": "^1.21.4",
"regenerator": "^0.6.3",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.10.0"
},

@@ -25,0 +27,0 @@ "engines": {

@@ -62,4 +62,6 @@ # suspend

* [suspend.async(fn*)](#suspendasyncfn)
* [suspend.promise(fn*)](#suspendpromisefn)
* [suspend.fn(fn*)](#suspendfnfn)
* [suspend.run(fn*)](#suspendrunfn-cb)
* [suspend(fn*)](#suspendfn)
* **[Suspending and Resuming Execution](#suspending-and-resuming-execution)**

@@ -100,2 +102,22 @@ * [yield](#yield)

### `suspend.promise(fn*)`
Accepts a generator function `fn*`, and returns a wrapper function that returns a Promise. If a value is returned (or the generator function completes with no explicit return value), the promise is resolved. If an error is thrown, then the promise is rejected.
**Example:**
```javascript
var getFavoriteNumberByUsername = suspend.promise(function*(username) {
var user = yield UserModel.find({ username: username });
return user.favoriteColor;
});
// the resulting function exposes a promise API:
var promise = getFavoriteNumberByUsername('jmar777');
```
Note that the above example also demonstrates the ability to yield promises, which is [documented below](#promises).
---
### `suspend.fn(fn*)`

@@ -137,2 +159,6 @@

### `suspend(fn*)`
An alias for [`suspend.fn(fn*)`](#suspendfnfn).
## Suspending and Resuming Execution

@@ -139,0 +165,0 @@

var assert = require('assert'),
suspend = require('../'),
run = suspend.run,
Q = require('q');
Promise = require('../node_modules/promise/lib/es6-extensions');

@@ -58,15 +58,15 @@ describe('yielded promises', function() {

function asyncDouble(num) {
var deferred = Q.defer();
setTimeout(function() { deferred.resolve(num * 2); }, 20);
return deferred.promise;
return new Promise(function(resolve, reject) {
setTimeout(resolve.bind(null, num * 2), 20);
});
}
function slowAsyncDouble(num) {
var deferred = Q.defer();
setTimeout(function() { deferred.resolve(num * 2); }, 40);
return deferred.promise;
return new Promise(function(resolve, reject) {
setTimeout(resolve.bind(null, num * 2), 40);
});
}
function asyncError() {
var deferred = Q.defer();
setTimeout(function() { deferred.reject(new Error('oops')); }, 20);
return deferred.promise;
return new Promise(function(resolve, reject) {
setTimeout(reject.bind(null, new Error('oops')), 20);
});
}
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