How to use p-throttle - 5 common examples

To help you get started, we’ve selected a few p-throttle examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github poketo / poketo / src / adapters / manga-here.js View on Github external
// @flow

import cheerio from 'cheerio';
import moment from 'moment-timezone';
import throttle from 'p-throttle';
import errors from '../errors';
import utils, { invariant } from '../utils';
import type { SiteAdapter, ChapterMetadata, Page } from '../types';

const TZ = 'America/Los_Angeles';

const t = $el => $el.text().trim();
const throttledGetPage = throttle(utils.getPage, 1, 600);
const trimLeadingZeroes = str => str.replace(/^0+(\d)/g, '$1');

function extractArgs(str) {
  // NOTE: This is a brittle approach, relying on the fact this variable is
  // named 'p'. That said, it's surprisingly easy and works.
  const startArgs = `return p;}('`;
  const argsStartIndex = str.indexOf(startArgs) + startArgs.length - 1;
  const argsEndIndex = str.indexOf('))', argsStartIndex);

  return str.substring(argsStartIndex, argsEndIndex).split(',');
}

function extractKeyFromCode(str) {
  // Removes the non- part of the string: ''.split('|')
  return str.replace(/\.split\('\|'\)$/, '').replace(/^'|'$/g, '');
}
github opencollective / opencollective-api / cron / daily / send-tax-form-requests.js View on Github external
const init = async () => {
  console.log('>>>> Running tax form job');
  // Filter unique users
  const users = findUsersThatNeedToBeSentTaxForm({
    invoiceTotalThreshold: US_TAX_FORM_THRESHOLD,
    year,
  });

  if (process.env.DRY_RUN) {
    console.log('>> Doing tax form dry run. Emails of users who need tax forms:');
    return users.map(
      pThrottle(
        user => {
          console.log(user.email);
        },
        MAX_REQUESTS_PER_SECOND,
        ONE_SECOND_IN_MILLISECONDS,
      ),
    );
  } else {
    return users.map(
      pThrottle(
        user => {
          console.log(`>> Sending tax form to user: ${user.email}`);
          return sendHelloWorksUsTaxForm(user);
        },
        MAX_REQUESTS_PER_SECOND,
        ONE_SECOND_IN_MILLISECONDS,
github poketo / poketo / src / adapters / manga-rock.js View on Github external
// @flow

import throttle from 'p-throttle';
import errors from '../errors';
import utils, { invariant } from '../utils';
import type { SiteAdapter } from '../types';

const throttledGetJSON = throttle(utils.getJSON, 1, 600);
const getIdFromOid = oid => oid.split('-').pop();
const parseTitle = (
  input: string,
): { title: ?string, chapterNumber: ?string, volumeNumber: ?string } => {
  const parts = input.split(': ');
  const title = parts[1];

  let chapterNumber;
  let volumeNumber;

  try {
    chapterNumber = utils.extractText(/chapter\s+([\d.]+)/i, parts[0]);
  } catch (err) {}

  try {
    volumeNumber = utils.extractText(/vol\.\s*([\d.]+)/i, parts[0]);
github poketo / poketo / src / adapters / mangadex.js View on Github external
// @flow

import he from 'he';
import moment from 'moment-timezone';
import throttle from 'p-throttle';
import errors from '../errors';
import get from '../get';
import utils, { invariant } from '../utils';
import type { ChapterMetadata, SiteAdapter } from '../types';

const throttledGet = throttle(utils.getJSON, 5, 500);

const LanguageCodes = {
  ENGLISH: 'gb',
  SPANISH: 'es',
  RUSSIAN: 'ru',
};

const StatusCodes = {
  '1': 'ONGOING',
  '2': 'COMPLETED',
};

/*
 * Since Poketo has no notion of languages or multiple versions of a chapter,
 * we'll just return the English version. Sorry, international peeps :(
 */
github poketo / poketo / src / adapters / manga-stream.js View on Github external
// @flow

import cheerio from 'cheerio';
import moment from 'moment-timezone';
import throttle from 'p-throttle';
import errors from '../errors';
import utils, { invariant } from '../utils';
import type { SiteAdapter } from '../types';

const TZ = 'UTC';
const throttledGet = throttle(utils.getPage, 5, 100);

const getChapterTimestamp = str => {
  let date;

  switch (str.toLowerCase()) {
    case 'today':
      date = moment.tz(TZ);
      break;
    case '1 day ago':
    case 'yesterday':
      date = moment.tz(TZ).subtract(1, 'day');
      break;
    case '2 days ago':
      date = moment.tz(TZ).subtract(2, 'days');
      break;
    default:

p-throttle

Throttle promise-returning & async functions

MIT
Latest version published 22 days ago

Package Health Score

82 / 100
Full package analysis

Popular p-throttle functions