GithubHelp home page GithubHelp logo

pets.js's Introduction

Pets.js

I built this in a few hours in the middle of the night so please forgive how crappy this is.

JS is a pretty terrible language but we have to live with it. Even after reading Crockford you're still stuck with its crappy evented I/O. Well, evented I/O isn't bad in and of itself, but without some sort of coroutine (like goroutines, tasklets or generators, which most reasonable languages implement these days) it's hard to do multiple rounds of fetching. Also it's really hard for newbies to grasp which sucks when they are trying to do a hackathon or something.

So I came up with this terrible idea that makes your code look synchronous. Basically it's just a special way to invoke functions that expect callbacks as if they were synchronous. How is this black magic accomplished? Well basically when you invoke a function we construct a unique key and check if we've fetched it yet. If we have we don't actually call that function and instead we just return the result. If we have not yet called it, we queue the fetch and throw an exception to bail out. When the fetches come back, we call the function again from the beginning. Keep looping until it's all done. Brilliant!

Future work

Build a Deferred-based abstraction on this maybe?

FAQ

  • Wait, my code gets run multiple times? What about side effects!

Yeah, sorry. Just don't put any side effects in your fetching code until after the last call to invoke().

  • What about Step.js / Async.js?

Confusing. I want to be able to explain it to an undergrad in 5 minutes. But probably better solutions for your production code since this is just silly.

  • What about Streamline.js?

Cool, but does desugaring which is kind of icky.

  • Why doesn't JS just implement generators?

It's in Harmony and Firefox has them, but the v8 guys don't want to implement them I guess. And v8 is awesome.

  • I only care about Node, is there some magic C++ hack that is better than this?

https://github.com/laverdet/node-fibers

If you're doing that much fetching though, are you sure you're using the right tool for the job? :P

  • Hey, is there some language better than JavaScript which makes this easier?

http://www.python.org/

pets.js's People

Contributors

petehunt avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.