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

async-promise-proxy

v0.0.2

Published

Using es6 Proxy, Promise and function generators to create a dynamic wrapper that generates promises from yields

Downloads

8

Readme

Usage

npm install async-promise-proxy

GeneratorToPromise

Used to wraps a es6 class that has generator methods, and proxies it into a promise. This is better expained with an example:

es6 exmpale

import req from 'some-request-library-that-supports-promises(like request-promise)';
import {GeneratorToPromise} from 'async-promise-proxy';

// NOTE this example is just to show case the yield -> then functionality.  This is not a great use-case.
class UserApi {
	* getUser(userId) {
		// in this instance think of yield* like await in the async/await C# 6.
		let user = yield* req.get({
			uri: `whatever.com/users/${userId}`
		});

		return user;
	}

	* postUser (user) {

		// one yield
		yield* req.post({
			uri: `whatever.com/`,
			json: user
		});

		// two yield, this will be the data in the .then(function(data) { })
		return yield* req.get({
			uri: 'whatever.com/${userId}'
		});
	}
}

// now we user the generator:

var userApi = new GeneratorToPromise(new UserApi());

// Now the calls to the generators will instead return promises.
userApi
	.getUser('someUserId')
	.then(function (data) {
		/* user data, that was returned in the UserApi.getUser */
		console.log(data);
	})
	.catch(function (err) {
		/* if an error happens this will be called */
		console.log(err);
	})

userApi
	.postUser({ userId: 'someUserId', name: 'someUser' })
	.then(function (data) {
		/* now this will be the last yield in the UserApi.postUser function, not the first yield */
		console.log(data);
	});

// You can even chain methods together:
someFakeApi
	.someMethodThatUpdatesTheUnderlyingInstance()
	.someOtherMethodThatUsesTheUpdatesFromThePreviousMethod()
	.then(console.log)
	// you can even chain off the thens
	.theLastMethodThatsCalled()
	.then(console.log);

NoMethodCatcher

Inherit from this class to setup a noSuchMethod like behavior in your class. Note this only works with node/io.js/)any server side js engine that supports the Proxy behavior). For node.js however you don't need to run with the --harmony-proxies flag, including the library does that for you.

es6 example:


import {NoMethodCatcher} from 'async-promise-proxy';

// class
class IAcceptAllMethods extends NoMethodCatcher {
	// these classes need constructors
	constructor() {
		super(); // call super(true) if your catch-all method is a generator.
		this.setup(this.catchTheMethods);

		// this is SUPER important, you must return this.proxy().
		return this.proxy();
	}

	// name will be the name of the unhandled method that got called, args is...the args.
	catchTheMethods(name, args) {
		console.log(`got unhandled method: ${name} with args ${args.join(", ")}`);
	}

	actualMethod() {
		console.log("Got a call on the actual method.");

		//note: if you want a fluent interface you have to return this.proxy() and not this
		return this.proxy();
	}
}

var test = new IAcceptAllMethods();

test.someMethodThatDoesntExist('does', 'not', 'exist') // --> got unhandled method: someMethodThatDoesntExist with args does, not, exist
test
	.actualMethod() // --> Got a call on the actual method.
	.soWhat('ever') // --> got unhandled method: soWhat with args ever

es5 example:

var NoMethodCatcher = require('async-promise-proxy').NoMethodCatcher;

function IAcceptAllMethods() {
	this.setup(this.catchTheMethods);

	return this.proxy();
};

// the only real difference.
IAcceptAllMethods.prototype = new NoMethodCatcher();

IAcceptAllMethods.prototype.catchTheMethods = function (name, args) {
	console.log("got unhandled method: "+name+" with args "+args.join(", "));
};

IAcceptAllMethods.prototype.actualMethod = function () {
	console.log("Got a call on the actual method.");

	//note: if you want a fluent interface you have to return this.proxy() and not this
	return this.proxy();
};


var test = new IAcceptAllMethods();

test.someMethodThatDoesntExist('does', 'not', 'exist') // --> got unhandled method: someMethodThatDoesntExist with args does, not, exist
test
	.actualMethod() // --> Got a call on the actual method.
	.soWhat('ever') // --> got unhandled method: soWhat with args ever