GithubHelp home page GithubHelp logo

cho45 / jsdeferred Goto Github PK

View Code? Open in Web Editor NEW
332.0 23.0 48.0 536 KB

Asynchronous library in JavaScript. Standalone and Compact.

Home Page: http://cho45.stfuawsc.com/jsdeferred/

Ruby 5.46% JavaScript 51.52% HTML 43.02%

jsdeferred's Introduction

THIS PROJECT IS OBSOLETED

You can use Promise standard object instead of JSDeferred now!

JSDeferred is born before Promise. It is no longer necessary to use JSDeferred because you can use the standard Promise now.

JSDeferred

See: http://cho45.stfuawsc.com/jsdeferred/

jsdeferred's People

Contributors

cho45 avatar drry avatar hotchpotch avatar kga avatar mattn avatar nanto avatar okuryu avatar piroor avatar yoko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsdeferred's Issues

bug in parallel method?

// in this code "d" may be not Deferred
str 456: if (typeof d == "function") d = Deferred.next(d); 

// in this code "d" always Deferred
str 474 :       for (var i in dl) if (dl.hasOwnProperty(i)) {
str 475 :           dl[i].cancel();
str 476 :       }

is a bug or I do not understand the code correctly?

Deferred.parallel = function (dl) {
    if (arguments.length > 1) dl = Array.prototype.slice.call(arguments);
    var ret = new Deferred(), values = {}, num = 0;
    for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
        if (typeof d == "function") d = Deferred.next(d);
        d.next(function (v) {
            values[i] = v;
            if (--num <= 0) {
                if (dl instanceof Array) {
                    values.length = dl.length;
                    values = Array.prototype.slice.call(values, 0);
                }
                ret.call(values);
            }
        }).error(function (e) {
            ret.fail(e);
        });
        num++;
    })(dl[i], i);

    if (!num) Deferred.next(function () { ret.call() });
    ret.canceller = function () {
        for (var i in dl) if (dl.hasOwnProperty(i)) {
            dl[i].cancel();
        }
    };
    return ret;
};

Deferred.define()してないとエラーになる

Deferred.define()

しないで
Deferred.parallel([
function () { return "hoge"; },
]).next(function (values) {
alert(values);
});
を実行すると、「nextが定義されていない」という内容のエラーになってしまいます。

コードを読むと、Deferred.parallelとDeferred.chainは、どうやらDeferred.define()されていることが前提で書かれているようです(ほかにもあるかもしれません)。

修正していただけると幸いです。

Append future function

/**
 * Create future object
 *
 * @example
 *   var token = future(future () {
 *     return $.getJSON('/api/token').next(function (data) { return data.token });
 *   });
 *
 *   token.next(function (token) {
 *     alert(token);
 *   });
 *
 *   token.next(function (token) {
 *     console.log(token);
 *   });
 *
 *   token.next(function (token) {
 *     // Use token
 *   }).
 *   error(function (e) {
 *     alert(e);
 *   });
 *
 * Future is like Deferred object but it behaves as "future" value (deferred process and future value).
 * Actually, this object is for a recycling value of Deferred.
 *
 * @param {function():Deferred} function returns Deferred which has a value for recycling.
 * @return {{next:function()}} Future object is has only 'next' function which return Deferred object.
 */
function future (fun) {
    var d = fun();
    var state, value;
    var waiting = [];

    d.
        next(function (val) {
            state = "call";
            value = val;
        }).
        error(function (err) {
            state = "fail";
            value = err;
        }).
        next(function () {
            for (var i = 0, it; (it = waiting[i]); i++) {
                it[state](value);
            }
        });

    return {
        next : function (cb) { /* return new Deferred */
            var d = new Deferred();
            d.next(cb);
            if (state) {
                d[state](value);
            } else {
                waiting.push(d);
            }
            return d;
        }
    };
}


var val = future(function () {
    return Deferred.wait(0.5);
});

val.next(function (v) {
    console.log(v);
    val.next(function (v) {
        console.log(v);
    });
});

chainの例が最後まで実行されない

AS移植していたのですが、chainの例を実行してみると最後まで実行されない現象を確認しました。
nodeで確認するとJSDeferred側のchainも最後まで実行されないようです。

jsdeferred/jsdeferred.js at master · cho45/jsdeferred
jsdeferred.jsの333行目

  {
    foo: wait(1),
    bar: wait(1)
  },

オブジェクトの中のwaitが即時実行されているようだったのでfunction () {} で囲ってあげると最後まで実行されました。

  {
    foo: function () {
      return wait(1);
    },
    bar: function () {
      return wait(1);
    }
  },

直し方としてあってますでしょうか?

Use XMLHttpRequest instead of GM_xmlhttpRequest in Google Chrome

Original Title: Doesn't work in Google Chrome

Google Chrome have no GM_xmlhttpRequest, but old good XMLHttpRequest instead.

So, http://github.com/cho45/jsdeferred/blob/master/jsdeferred.userscript.js doesn't work.

Google Chrome's XMLHttpRequest can do x-domain requests. jsdeferred.userscript.js could try to do XMLHttpRequest, if no GM_xmlhttpRequest available.

Right now, I can't port diff_for_gist.user.js to Chrome because of jsdeferred.userscript.js.

jQuery binding should treat jQuery.Deferred as our Deferred.

  • If jQuery.Deferred was returned from callback, JSDeferred should wait it.
  • Function parallel should accept jQuery.Deferred
  • Function earlier should accept jQuery.Deferred
  • Binding should implement function which converts jQuery.Deferred to our Deferred object.

How to access previous Ajax response in a chain of ajax requests

Hi,

I'm writing a javascript client for a webservice that requires a lot of inter-dependent requests (i.e. you need the parts of the response from request1 as GET parameters in request 2). To avoid the extensive use of nested callbacks, I like to develop the client using jquery/jsdeferred's next statement. To make my code even shorter, I like to register my custom request/response handler as Deferred shorthand function. Here is what I have so far:

Deferred.define();

function makeRequest (method, params){
    $.extend(params,{method: method, api_key: 123});
     var url = $.param('http://api.example.com', params);
    return next($.get(url))
}

Deferred.define(makeRequest, ["next","parallel"]);


makeRequest('getIdForUsername', {username:'dummy'}).
//user_id is from the response of 'getIfForUsername' request
makeRequest('getUserStatus', {user_id:user_id}).
next(function(userstatus){
    //generate HTML for user status
});

My questions:

  • how can I "pass on" the user_id obtained in the first request to parameters of the second request?
  • How can I differentiate between parallell and sequential execution of the makeRequest- function?
  • How can I implement error handling proberly? (e.g. request1 gives an error response --> chain should stop and user should be notified of bad response)

Thanks for developing jsdeferred!

Franz

Image corrupt or truncated

First, thank you for the fine script.
If I run a script in Firefox I see "Image corrupt or truncated: data:image/png.." at the Javascript error console.
This also happens on your demo page. In other browsers it seems okay.

IEでSSL通信時に警告が出ることがある

IE8において(恐らくIE6も)、httpsのページでJSDeferredを使った場合に、Deferred.nextを連続で呼ぶと、「セキュリティで保護された Web ページ コンテンツのみを表示しますか」が発生します。

再現するコード例(このHTMLをhttpsでアクセス): http://gist.github.com/205896

原因は、Deferred.next_faster_way_readystatechange で作っている<script>タグだと思います。

src="javascript:" ではなく、 src="" として<script>タグを生成するとこの警告は止まりましたが、DOMの扱いとして正しいかちょっと自信がないです。

Wrong process sequence with using Deferred.connect

// --------------- Here order of log is 2 -> 1 , child redirect works wrong

function showData(data) {
    var fadeout = Deferred.connect($.fn.fadeOut, { target: $('#container'), ok:1 });
    return fadeout(500).
    next(function() {
        return $('#container')
            .text(data)
            .deferred("fadeIn", 2000)
            .next(function() { console.log(1) })
    }).
    next(function() { console.log(2) })
}

Bug in parallel method

Deferred.parallel([ 1, 2, 3 ].map(function(i) { 
    var d = new Deferred; 
    setTimeout(function() { 
        d.fail(i); 
    });
    return d;
})).error(console.log.bind(console));
// => 1
// => 2
// => 3

Since you will get only one result for a succeeded paralleled-deferred I guess there shouldn't be three results for a failed paralleled-deferred.

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.