Skip to content

Instantly share code, notes, and snippets.

@daleharvey
Last active August 29, 2015 13:56
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 daleharvey/9198141 to your computer and use it in GitHub Desktop.
Save daleharvey/9198141 to your computer and use it in GitHub Desktop.
// How it should be written
remote.bulkDocs({docs: docs});
db.replicate.from(remote);
var docs = db.allDocs();
res.total_rows.should.equal(num);
// No error handling
remote.bulkDocs({ docs: docs }, function (err, info) {
db.replicate.from(remote, {}, function () {
db.allDocs(function (err, res) {
res.total_rows.should.equal(num);
done();
});
});
});
// check every err
remote.bulkDocs({ docs: docs }, function (err, info) {
if (err) return done(err);
db.replicate.from(remote, {}, function (err) {
if (err) return done(err);
db.allDocs(function (err, res) {
if (err) return done(err);
res.total_rows.should.equal(num);
done();
});
});
});
// bind every callback
remote.bulkDocs({ docs: docs }, bindSuccess(done, function (err, info) {
db.replicate.from(remote, {}, bindSuccess(function (err) {
db.allDocs(bindSuccess(function (err, res) {
if (err) return done(err);
res.total_rows.should.equal(num);
done();
}));
}));
}));
// promises
remote.bulkDocs({docs: docs}).then(function() {
return db.replication.from(remote);
}).then(function() {
return db.allDocs();
}).then(function(res) {
res.total_rows.should.equal(num);
done();
}).catch(done);
@nick-thompson
Copy link

function iter(gen) {
  var it = gen();
  (function next(res) {
    var f = it.next(res);
    f.call(null, function(err, res) {
      if (err) return done(err);
      next(res);
    });
  })(null);
}

it('should be sweet', iter(function* () {
  yield remote.bulkDocs.bind(remote, {docs: docs});
  yield db.replicate.from.bind(db, remote);
  var res = yield db.allDocs.bind(db);
  res.total_rows.should.equal(2);
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment