GithubHelp home page GithubHelp logo

em-twistedlike's Introduction

em-twistedlike

This Monkeypatch implement some of Twisted's defer funtionnalities to EventMachine Deferrable. This provide convenient helper and also a better exception management during callbacks execution, ie the double callback cross-chains of Twisted.

Each Exception raised during this chain execution is wrapped into a Failure object and passed to the next error callback. The original exception can be retrieved with Failure#value.

I suggest you to be familiar with the behavior of Twisted. For more information, see http://twistedmatrix.com/documents/current/core/howto/defer.html#visual-explanation.

## Main features

  • New functions Deferrable#add_callback, Deferrable#add_errback, Deferrable#add_both, Deferrable#add_callbacks that manage callbacks with error management, the Twisted way.
  • DeferrableList object to merge multiple Deferrable.
  • Automatically wrap Exception raised within callbacks and params sent to Deferrable#fail into a Failure object.
  • New helpers DefaultDeferrable#failed and DefaultDeferrable#succeeded to create already fired Deferrable.

Example

require 'em-twistedlike'

EM.run {

    d = EM::DefaultDeferrable.new
    d.add_callback { |result|
        puts result
        raise "An error"
    }

    d.add_errback { |failure|
        puts "Errback 1: #{failure}"
        failure # Forward the error to the next errback
    }

    d.add_callback { |result|
        puts "This is not executed"
    }

    d.add_errback { |failure|
        puts "Errback 2: #{failure.value}"
        "Error is resolved" # This is the result for the next callback
    }

    d.add_callback { |result|
        puts "Final result: #{result}"
    }

    d.add_both { |result_or_failure|
        EM.stop
    }

    EM.next_tick {
        d.succeed("The result")
    }
}

Will produce the output :

The result
Errback 1: RuntimeError - An error
Errback 2: An error
Final result: Error is resolved

License

Copyleft 2015 - Nicolas AGIUS

This module is released under the terms of the GNU GENERAL PUBLIC LICENSE version 3.

em-twistedlike's People

Contributors

nagius avatar

Watchers

 avatar James Cloos avatar

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.