New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

consec

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

consec

Consecutively iterate generator functions

1.1.0
latest
Source
npm
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

consec

Wrap a bunch of async operations into one function and iterate each step consecutively seamlessly returning a promise resolving with the end returned value. If an error is throw along the way or a yield promise fails then the promise rejects allowing you to catch the error.

Installation

$ npm install consec

Usage

var consec = require('consec')

consec(function * () {
  var res1 = yield Promise.resolve('abc')
  return yield Promise.resolve(res1)
}).then(function (res) {
  res // => 'abc'
})

// you can also yield regular values
consec(function * () {
  var res1 = yield 'abc'
  return yield res1
}).then(function (res) {
  res // => 'abc'
})

// you can pass in the function context and parameters
var ctx = {}
function * gen(a, b, c) {
  this // => ctx
  return yield ['a', 'b', 'c']
}

consec.call(ctx, gen, 'a', 'b', 'c')
.then(function (abc) {
  abc // => ['a', 'b', 'c']
})

// if an error is throwm then the chain ends there
var error = new Error()
consec(function * () {
  throw error
}).catch(function (err) {
  err // => error
})

//  if a promise is rejected then the chain ends there
var error = new Error()
consec(function * () {
  yield Promise.reject(error)

  // this is never reached
  return 'abc'
}).catch(function (err) {
  err // => error
})
  
// a generator can also be yielded
function * g() {
  return yield Promise.resolve('abc')
}
consec(function * () {
  var a = yield * g()
  t.equals(a, 'abc')
})

// finally you can even nest consec calls inside one another
function * g(abc) {
  return yield Promise.resolve(abc)
}

return consec(function * () {
  return yield consec(g, 'abc')
}).then(function (res) {
  res // => 'abc'
})

Keywords

generators

FAQs

Package last updated on 03 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts