Git Product home page Git Product logo

nodejs-twisted-deferreds's Introduction

Build Status

Direct port of python's Twisted Deferred module to coffeescript and thus javascript.

Links:

Installation

git clone git://github.com/my8bird/nodejs-twisted-deferreds.git 
cd nodejs-twisted-deferreds
(sudo) npm link

Dependencies:

  • coffeescript
  • nodeunit (for running the tests)

Usage

This is a contrived example that reads the contents of a file, trims the whitespace and prints the result to the console. If an error occurs then it is logged.

fs    = require('fs')
defer = require('twisted-deferred')
Deferred = defer.Deferred

# Create Deferred instance that will track the steps
d = new Deferred()

# Add a step that cleans the content
d.addCallback (content) ->
    return content.trim()

# Add a step that uses the cleaned content
d.addCallback (cleanedContent) ->
    console.log cleanedContent

# If there is an error at any step make sure it is logged.
d.addErrback (err) ->
    console.error err

# Grab the content from the file and start the procedure
fs.readFile "path", "r", (err, data) ->
    if err
        d.errback err
    else
        d.callback data

It is also possible to wrap existing async code using the toDeferred method: d = defer.toDeferred(fs.readFile, "path") d.addCallback (content) -> console.log content d.addErrback (err) -> # log err

Tests

To run the tests ensure that nodeunit is installed. Then run run-tests.sh.

nodejs-twisted-deferreds's People

Contributors

my8bird avatar tomjnixon avatar

Stargazers

Jonathan Barratt avatar  avatar Luci Brăn avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

nodejs-twisted-deferreds's Issues

If some Deferred using as value of second Deferred - not need call callback of second Deferred

Hello.
Sorry for my very bad english.

I have a code like this:

var twisted = require('twisted-deferred');
var a = new twisted.Deferred();
a.addCallbacks(function(a) {
    console.log('a = ' + a);
});
var b = new twisted.Deferred();
b.addCallbacks(function(b) {
    console.log('b = ' + b);
});
console.log('start');
a.callback(b);
console.log('process');
b.callback(123)
console.log('end');

And I expect as result this:

start
process
b = 123
a = 123
end

But keep this:

start
a = [object Object]
process
b = 123
end

In other words, if i set value to Deferred a as another Deferred b, not need call callback for a, until b is unknown

I believe that tцisted and works, but I could be wrong

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.