npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

thot-gen

v0.2.5

Published

Generator control flow based on bluebird

Downloads

7

Readme

thot-gen

High performance bluebird based generator control flow library

Build Status Dependency Status devDependency Status unstable

Basic Usage

var gen = require('thot-gen');
var run = gen.run;

run(function *(){
  yield 'some value';
  yield true;
  yield thunk;
  yield gen.resume(nodeStyleAsyncCallback)(1, 2);
  yield [1, 2];
  yield {some: 'value'};
})(function(err, result){});

or

var gen = require('thot-gen');
var run = gen.run;

run(function *(){
  yield 'some value';
  yield true;
  yield thunk;
  yield gen.resume(nodeStyleAsyncCallback)(1, 2);
  yield [1, 2];
  yield {some: 'value'};
})().then(function(result){}, function(err){});

##API##

###run### thotgen.run(genFunc);

Run is the main method of thot-gen. It turns a generator function into a function that can be called at any point.

When calling the function returned by run you can pass in an optional nodestyle callback method or if you don't the method will return a promise.

All of the promises are based on bluebird for performance and flexibility reasons. This allows you to easily add a lot of interesting control flow ontop of thot-gen without a lot of additional work.

If you would like to control the context that the generator is run in just call the returned method using call and passing in the context.

run(function*(){
  yield this;
}).call({some: 'object'});

###resume### thotgen.resume(func);

This method will turn a regular node-style callback into a thunked method. It's a convenience method that uses thunkify to convert the function.

###resumeRaw### thotgen.resumeRaw(func);

This method is meant to be used with functions that don't follow the traditional nodestyle callback syntax of err, result or that return multiple results. This method makes no assumptions about what it will get back and will just return all values returned to the callback as an array. Be sure that you check for errors properly based on the function that you are using

###delay### thotgen.delay(milliseconds);

This method is a shortcut to allow you to delay the execution of a generator by yielding the value

yield delay(500); will delay execution for 500 milliseconds.

###timeout### thotgen.timeout(milliseconds, [string errormessage]);

This is a method on bluebird that allows you return an error for the promise if it doesn't complete within the specified amount of time

To take advantage of this method you do need to use the returned promise after running the couroutine

thotgen.run(function*(){
  yield 'some possibly long running method here';
})().timeout(500, 'not fast enough');

###cancel### thotgen.cancel([string errormessage]);

This method is very similar to timeout above but allows you to manual cancel the promise and return the error.

var prom = thotgen.run(function*(){
  yield 'some possibly long running method';
})();
prom.cancel('changed my mind');

Cancel will not cancel a promise that has already been resolved.

##Bluebird## Since thot-gen is built on top of bluebird you can use any of the additional methods that bluebird provides for a full list and documentation please see bluebirds documentations https://github.com/petkaantonov/bluebird/blob/master/API.md