Skip to content

Instantly share code, notes, and snippets.

@kfatehi
Created July 7, 2014 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfatehi/80a341f0a92445a41ef1 to your computer and use it in GitHub Desktop.
Save kfatehi/80a341f0a92445a41ef1 to your computer and use it in GitHub Desktop.
oh javascript, how you nest so deeply, so easily...
function createCardsViaProvider(req, res, next) {
var board = req.board;
var handler = providers[req.params.provider].cardHandler;
var promises = [];
Column.findOne({ board: board._id, role: 1 })
.exec(function (err, column) {
if (err) {
logger.error(err.message);
res.send(500);
} else {
handler.batchImport(board, req.body, function (attributes) {
attributes.column = column._id;
promises.push(Card.create(attributes))
}, function() {
Promise.all(promises).then(function () {
Board.update({ _id: board._id }, { columns: board.columns }, function(err) {
if (err) { res.send(500, err.message); }
else {
Board.findOne({ _id: req.params._id }).populate('columns').exec(function(err, board) {
if (err) {
logger.error(err.message)
res.send(500)
} else if (board) {
Card.populate(board.columns, { path: 'cards' }, function(err) {
if (err) {
logger.error(err.message);
/*lol javascript*/ res.send(500);
} else {
req.board = board;
res.send({ board: { columns: board.columns } })
}
});
} else {
res.send(404);
}
});
}
});
});
})
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment