AsyncResult and handling rollback

Hi,

Trying to implement 2PC Transaction Like workflow in F# (see http://learnmongodbthehardway.com/article/transactions/) and hitting an issue with computation expressions (asyncResult for example) and rollback.

If you have the following pseudocode:

let rollbackWorkflow parX parY =
… here calling rollbackService1 and rollbackService2

let executeWorkflow par1 par2 par3 =
asyncResult {
let! result1 = callService1 x y z
let! result2 = callService2 x2 y2 z2
}

how can I check in executeWorkflow if result1 and/or result2 is Error and then call rollbackWorkflow function?? Should I change callService1 and callService2 to raise exceptions instead of returning results also in expected error cases (not sufficient funds, limits exceeded), or should I use some function like teeError? Any suggestion highly appreciated!

Best regards,
Deyan

P.S. This is what I want to eventually implement:

function executeTransaction(from, to, amount) {
var transactionId = ObjectId();

transactions.insert({
_id: transactionId,
source: from,
destination: to,
amount: amount,
state: “initial”
});

var result = transactions.updateOne(
{ _id: transactionId },
{ $set: { state: “pending” } }
);

if (result.modifiedCount == 0) {
cancel(transactionId);
throw Error(“Failed to move transaction " + transactionId + " to pending”);
}

// Set up pending debit
result = accounts.updateOne({
name: from,
pendingTransactions: { $ne: transactionId },
balance: { $gte: amount }
}, {
$inc: { balance: -amount },
$push: { pendingTransactions: transactionId }
});

if (result.modifiedCount == 0) {
rollback(from, to, amount, transactionId);
throw Error(“Failed to debit " + from + " account”);
}

// Setup pending credit
result = accounts.updateOne({
name: to,
pendingTransactions: { $ne: transactionId }
}, {
$inc: { balance: amount },
$push: { pendingTransactions: transactionId }
});

if (result.modifiedCount == 0) {
rollback(from, to, amount, transactionId);
throw Error(“Failed to credit " + to + " account”);
}

// Update transaction to committed
result = transactions.updateOne(
{ _id: transactionId },
{ $set: { state: “committed” } }
);

if (result.modifiedCount == 0) {
rollback(from, to, amount, transactionId);
throw Error(“Failed to move transaction " + transactionId + " to committed”);
}

// Attempt cleanup
cleanup(from, to, transactionId);
}

executeTransaction(“Joe Moneylender”, “Peter Bum”, 100);