Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightweight implementation of promises

I am building a simple charting tool. When a user chooses a data source and a chart type, both the data and the script for the specific chart (.js) are loaded and then the code renders the chart.

Currently I use the following sequence:

  1. Load the chart script
  2. Load the data
  3. Render the chart

To improve performance, I'd like to implement promises and load script and data in parallel. A library like jQuery is too big for my needs (the point of the question is not to argue on this), are there more lightweight solutions? Maybe a 1-2 kb library, or a tutorial? Again, this is a very basic implementation with just two actions in parallel.

[Update] I upvoted all the replies as they all look excellent. I'll report back after I do some more testing.

like image 442
Christophe Avatar asked Oct 16 '12 21:10

Christophe


1 Answers

I strongly suggest using an implementation of the Promises/A specification, which is becoming the standard way of doing promises in JavaScript. Promises work better when everyone uses the same flavor, so it's in everyone's interest to use compatible implementations.

Q is probably the most popular and fully featured implementation (it also will adapt jQuery and other incompatible promises), while when and rsvp are supposed to be more "lightweight".

like image 137
tlrobinson Avatar answered Sep 28 '22 13:09

tlrobinson