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

tasyncexpress

v2.0.0

Published

monkeypatch express/router/layer so you can write async handler

Downloads

9

Readme

tAsyncExpress

monkeypatch express/router/layer so you can write async handler

examples

app.post('/user', ({body})=>userDao.insert(body));
app.post('/user/login', async (req,res)=>{
    const user = await userDao.getByMail(req.body.mail);
    if(user.password == req.body.password){
        req.session = {userId:user.id};
        return user;
    }
    return false;
});
app.get('/user/:id', ({params:{id}})=>userDao.getById(params.id));
app.get('/blogpost/:title', async ({params:{title}})=>{
    var post = await mysql.query('SELECT * FROM blogposts WHERE title = *', title);
    post.author = await mysql.query('SELECT * FROM user WHERE id = ?', post.authorId);
    return post;
})
// and all your existing code also still works, still run sum tests before go blind to production

sourcecode

require('express/lib/router/layer').prototype.handle_request = function handle(req, res, next) {
    var fn = this.handle;
    if (fn.length > 3) { return next(); }
    try {
        var promise = fn(req, res, next);
        if (typeof (promise) === 'object' && typeof (promise.catch) === 'function' && typeof (promise.then) === 'function') {
            promise.then((data) => {
                try {
                    if (data !== undefined){
                        if(typeof(data) == 'string'){
                            res.send(data);
                        }else if(typeof(data)==='object'){
                            res.json(data);
                        }
                    } 
                } catch (err) {/*ignore error*/ }
            });
            promise.catch(next);
        }
    } catch (err) { next(err); }
}; 

comparisons

there are different aproaches: similar aproach with different opinion about handling the resolve value of that promise.

  • express-async-errors: not handling the response value of a promise

replacing the 'all','use','del',[verb] methods on the routers.

  • express-promise-router: also is handling promise resolve only with the values "next" and "route"
  • express-asyncify

Create a subclass

  • express-as-promised: is extending the Router class

let you wrap every handler into a promise handling method.

  • promised-routing
  • async-middleware
  • express-wrap-async
  • express-async-wrap
  • express-async-handler

At this point I am not clear what aproach will be in general the best, as still more projects get started with express then with koa.js I would also like to see a change in version express 5. Using this patch module, the code with express get much cleaner, so that there is very little reason to switch to an other framework. In this module you see the opinion of not just catching the error but also returning JSON.