Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interesting use of tasks #27

Open
benjamingr opened this issue Mar 7, 2015 · 1 comment
Open

Interesting use of tasks #27

benjamingr opened this issue Mar 7, 2015 · 1 comment

Comments

@benjamingr
Copy link

http://stackoverflow.com/a/28860929/1348195

@bergus
Copy link

bergus commented Mar 7, 2015

Here's a little different, maybe easier to understand implementation of the idea, with the common db connection example:

function getConnection(...args) {
    var promise = openConnection(...args); // internal method
    return promise.then(function(conn) {
        promise.then(function() { // will be called at the end of the turn, after all
                                  // callbacks that have been subscribed until now
            conn.close();
        });
        return conn;
    });
}

Now, conn has an internal counter that keeps track of the things that happened in the callbacks before conn.close() was called, so it may postpone the actual closing depending on them. Each of these things would either be synchronous (and not require postponing the disposal) or an asynchronous action, that would return a promise constructed in a similar manner like the above (with something different than openConnection()) that finally closes the connection when it's done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants