Open benjamingr opened 9 years ago
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.
http://stackoverflow.com/a/28860929/1348195