Giter VIP home page Giter VIP logo

simply-deferred's Introduction

Simply Deferred

jQuery-like Deferred API for Node and the browser

Simply Deferred is now feature complete. Bug fixes will be made, but no API changes are expected unless they're to ensure compliance with the jQuery API.

Installation

npm install simply-deferred

Usage

var Deferred = require('simply-deferred').Deferred;
var rendering = new Deferred();
rendering.done(function(){
    console.log('Finished rendering');
});

//...

rendering.resolve();

API

Simply Deferred is fullly compatible with jQuery's API, so the docs and usage are the same. Like the jQuery deferred API, it provides the following methods:

  • Deferred()
  • deferred.state()
  • deferred.done()
  • deferred.fail()
  • deferred.progress()
  • deferred.always()
  • deferred.promise()
  • deferred.notify()
  • deferred.notifyWith()
  • deferred.resolve()
  • deferred.resolveWith()
  • deferred.rejectWith()
  • deferred.reject()
  • deferred.pipe()
  • deferred.then()
  • Deferred.when()

Collaborating

Please feel free to raise issues on github.com/sudhirj/simply-deferred/issues - both obvious bugs or incompatibilities with jQuery are welcome.

If you'd like contribute a fix or a feature, that would be even better. Please make sure all pull requests are accompanied by tests, though.

If you'd like to start work on a feature that is not part of the jQuery library, just raise an empty pull request and let's talk about it first - the goal here for this library to be a drop-in replacement for jQuery, with the same docs and API.

Usage with Zepto

Zepto now has a deferred module available, so you might want to use that if Zepto is your primary reason for using Simply Deferred.

Simply Deffered also acts as a plugin to Zepto. The absence of a Deferred library was one of the biggest reasons I've been holding back, so I thought it made sense to write one. Once you have both Zepto and Simply Deferred on your page, just do Deferred.installInto(Zepto) to set it up. The installation makes the following changes to bring it closer to jQuery:

  • Aliases the Deferred constructor to $.Deferred.
  • Aliases the when method to $.when.
  • Wraps $.ajax to return a promise, which has only the following methods: state(), done(), fail() and always(). The arguments passed to the done and fail callbacks are the same ones passed to the success and error options.

simply-deferred's People

Contributors

ericmatthys avatar fukamachi avatar rraval avatar sudhirj avatar therabidbanana 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

simply-deferred's Issues

Which browsers do you support?

I can't find any info on which browsers are supported by Simply Deferred.

Could you please provide me this info any perhaps add it somewhere in your documentation?

Remove underscore.js dependency

Hello!
I'm using simply-deferred, simply because Zepto doesn't includes support for deferred.
But I got surprised it depends on underscore.js

I also like underscore, but I'm trying to remove all posible dependencies for my current project I'm working, and adding more dependencies would defeat the whole purpose of using Zepto.js in the first place ;)

If we want this on Zepto.js core, I think we should remove external dependencies.

What do you think?

.pipe() support

Is there a reason you've omitted jQuery deferred's pipe() method?

InstallInto not obvious

Please make the InstallInto function more obvious. It took me at least 15 minutes to figure out how to get to your when function.

btw, thanks for this library!

Cannot chain $.ajax() using .then()

I'm using chromium 26 browser in Gentoo Linux.
Suppose we have JavaScript code such as

Deferred.installInto(Zepto); // when using Zepto + simply deferred
var first = function(){
  return $.get("/data1")
}
var second = function(data1){
  console.log(data1)
  return $.get("/data2")
}
var third = function(data2){
  console.log(data2)
}
$.when(first()).then(second).done(third)

and each URL returns JSON as follows:
/data1 returns {"data":"one"}
/data2 returns {"data":"two"}

In jQuery 1.9.1, output of console is

{"data":"one"}
{"data":"two"}

This is the expected behavior at least for me.
However, output of Zepto 1.0.0 + simply-deferred is

{"data":"one"}
Object {state: function, done: function, ...}

Is this a bug? Or am I doing something wrong?
I want to get /data2 after getting /data1, and obtain the result of /data2.
Is there any way to do it in same code for jQuery and Zepto + simply-deferred ?

when doesn't trigger fail callback

It seems to me that when doesn't trigger a fail callback after all promises have been completed.

p1 = new Deferred(); p2 = new Deferred();
w = Deferred.when(p1, p2).then(function(){console.log('success');}, function(){console.log('failure'));
p1.resolve();p2.reject();

This doesnt trigger the fail function and w.state() is 'pending'
When I try p1.resolve(); p2.resolve() I get the success log and w.state() is 'resolved'

Looking at the code for _when, it seems that it only binds the after function to done and not to fail so the counter never reaches zero.

I've quickly patched my js version's after function to accept a failure flag and pass that flag to the done function. I've also patched the when function to call after with the flag set on failure.

nested when done parts are executed at the same time instead of waiting

$.when($("#title,#text,#nav,#footer,#impress").hide(),$("#text").css("height","0px"),$("#line_short,#line_long").css({"width":"0px","margin":"0"}),$("#title").fadeIn(t2)).done(function(){
    $.when($("#line_short").animate({width:"100%"},t1)).done(function(){
        $.when($("#text").show(),$("#text").animate({height:$("#mmm").height()},t15)).done(function(){
            $.when($("#nav").fadeIn(t1)).done(function(){
                $.when($("#line_long").animate({width:"100%"},t15)).done(function(){
                    $.when($("#footer,#impress").fadeIn(t1)).done(function(){$("#nav span").addClass("_nav");
                    });
                });
            });
        });
    });
});

They are all executed at once but not one after one.

Is this a bug?

Backbone Layoutmanager

Hi,
the backbone layoutmanager uses resolveWith, is there a way to add that back into your library?

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.