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

async-by-page

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-by-page

When you need to operate on a list of things, but you can only get 1 page at a time.

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

async-by-page

When you need to operate on a list of things, but you can only get 1 page at a time.

This might already exist, but it's something I've had to do a few times recently so it's time to make a lib.

Usage

const byPage = require('async-by-page')

byPage(fetchNextPage, (item, next) => {
  // Let's say we only want to operate on items
  // that have a certain flag set. We can just call `next()`
  // with no args to skip.
  if (item.isSubscribedToNewsletter) { return next() }

  // For the others, we can perform an action (like sending an email)
  // and then call `next()` when it's done.
  sendEmail(item, next)
})

// Here we define out "fetch next page" function, that is called from `async-by-page`.
// It takes a single callback argument. If the callback is called with an error,
// the iteration terminates.
//
// The main thing to know with this function is that it is stateful.
// It should keep track of its own state, and return the next page of results
// each time it is called.

let page = 1
function fetchNextBatch (cb) {
  getLeadsBatch(page, (err, res) => {
    page += 1
    return cb(err, res)
  })
}

Can I use it? Is it stable?

Not really :) I'm going to be trying a few different things first, and adding a couple of extra features (eg. option for running in parallel VS series). But your feedback is still welcome. I'd also like to write an implementation with pull-streams because I think it would be a very useful comparison (and probs much cleaner too!).

License

MIT

FAQs

Package last updated on 30 May 2017

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

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