Lightweight implementation of promises

后端 未结 7 1542
独厮守ぢ
独厮守ぢ 2021-02-02 17:09

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

相关标签:
7条回答
  • 2021-02-02 17:38

    I built something like this a while back that I called "when-then". I wanted something that would say, "Load all of these things, then do something when they're done".

    https://github.com/geuis/when-then

    I was inspired by the promises idea, but wanted something a little simpler for simple tasks.

    0 讨论(0)
  • 2021-02-02 17:39

    Check Deferred it has modular build, and you can decide just to take core and that shouldn't be larger than 1kb.

    Browser install instructions explains how to create such bundle in few easy steps

    0 讨论(0)
  • 2021-02-02 17:40

    If you want it really lightweight, You should try sb-promise, It's under MIT license. It's just a few kbs and is API compatible with native Promises.

    function myFunction(){
       return new Promise(function(resolve,reject){
         resolve({some:"data"});
       }
    }
    myFunction().then(function(result){
       console.log(result); // {some:"data"}
    });
    
    0 讨论(0)
  • 2021-02-02 17:48

    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".

    0 讨论(0)
  • 2021-02-02 17:53

    Wanted to throw in my 2¢ as there's been a development here. Promises have arrived natively in JavaScript and will be implemented for FF 30 and Chrome 33 (according to this table).

    Although that's hardly worth mentioning in a place that's usually flooded with "but does it support IE6?", there's a 2kb minified and gzipped polyfill here that is "basically an API remapping of rsvp.js."

    Since jQuery promises aren't really promises, and I agree with this guy that promise syntax is inconsistent across libraries, my view is that native promises are already worth implementing even though your users' browsers probably don't support them yet.


    Edit: After using Promises in the context of Angular and the Firefox addon-SDK, both of which are very similar to Q, I prefer that syntax and have read that it is still faster than the native implementation.

    0 讨论(0)
  • 2021-02-02 17:57

    Try async.

    It has methods to do operations in parallel, and in series. It was originally designed to run in node, but now works in browser as well.

    EDIT TO ADD: They don't have a minified version, so the full enchilada is ~42kb.

    0 讨论(0)
提交回复
热议问题