2
\$\begingroup\$

I'm hoping some folks can take a few minutes to review some code I was working on today.

https://gist.github.com/1115202

Essentially, I'm doing a lot of work in Node.js at the moment and I'm a big fan of Promises.

Since 99% of the work that I'm doing involves performing several asynchronous actions and then parsing the results, I wanted something a little simpler and more light-weight that some of the other Promise/Deferred/Futures libraries that are out there.

My aim with this is to have a simple when().then() format without having to instantiate a new Promise inside each function.

The usage is like this:

when(
 function(){ this.pass(1); }, 
 function(){ this.pass(2); }
)
.then(function(results){ 
 console.log( results ); 
});

In the Github gist I've linked to above, there are use cases near the bottom. Test case 1 uses timeouts, the 2nd test case uses several async http requests in Node.js and logs the total results at the end when all 3 have finished.

I'd love to hear any opinions good or bad, recommendations on how to make it better, etc.

asked Jul 30, 2011 at 5:14
\$\endgroup\$
4
  • \$\begingroup\$ I use After instead of when/then. I don't think you can get much lighter then that \$\endgroup\$ Commented Jul 30, 2011 at 13:01
  • \$\begingroup\$ @Raynos how do you prevent the same function from calling after return value twice, or is that not a worry? I mean the scenario of that happening is highly unlikely. \$\endgroup\$ Commented Jul 30, 2011 at 15:27
  • \$\begingroup\$ @Lime if you use after in a buggy manner then that's a bug you should fix. \$\endgroup\$ Commented Jul 30, 2011 at 15:37
  • \$\begingroup\$ In terms of shortening, I think you can replace the switch statement with a ternary function, unless I've missed something. gist.github.com/1115658 \$\endgroup\$ Commented Jul 30, 2011 at 15:58

1 Answer 1

3
\$\begingroup\$

I made The following edits:

  1. Prevent the same when function from calling pass twice(therefore invalidating results)
  2. Sends pass function as 1st argument to avoid var that = this
  3. It stores the first pass arg only for simplicity instead of all arguments in results(easily switched)
  4. Then function can't be called more then one
  5. I removed the need for When and when functions(you could leave the separate functions for clarity if you wanted)
  6. There is now only one method then and prop funcs

https://gist.github.com/1115319

Usage

when(
 function(pass){setTimeout(pass,3000)},
 function(pass){pass('2nd');}
)
.then(function(results){
 console.log(results);
})
answered Jul 30, 2011 at 8:00
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the tips Lime. Here's the updated gist gist.github.com/1115202/…. I adopted a few of your changes, including using instanceof to instantiate a new when() and having then() redefine itself. I also liked your method of passing pass() into each function as an argument, so I adopted that as well. I ended up more or less using the same style I started with, since its a bit easier for me to read through and understand the code when I need to go back to it, and hopefully others as well. What do you think of the updates? \$\endgroup\$ Commented Jul 30, 2011 at 23:28
  • 1
    \$\begingroup\$ It doesn't have some of the security/safety checks implemented above, but otherwise it looks good to me :D \$\endgroup\$ Commented Jul 31, 2011 at 4:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.