Created
August 15, 2014 15:24
-
-
Save calvinmetcalf/4d8d0c82079d6edec5bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="//cdn.jsdelivr.net/pouchdb/2.2.3/pouchdb.min.js"></script> | |
</head> | |
<body> | |
<script> | |
var db = new PouchDB('tasks'); | |
function saveTask() { | |
return db.post({ title: 'take out trash' }); | |
} | |
function getAndRemove(id) { | |
return db.get(id).then(function (object) { | |
return db.remove(object).catch(function (err) { | |
console.log('error: ' + err); // isn't executed | |
}); | |
}).catch(function (err) { | |
console.log('error: ' + err); // isn't executed | |
}); | |
} | |
function purge() { | |
return db.allDocs({include_docs: true}).then(function (doc) { | |
var promises = []; | |
doc.rows.forEach(function (el, i) { | |
promises.push(getAndRemove(el.doc._id)); | |
}); | |
return Promise.all(promises); | |
}); | |
} | |
function printAllDocs() { | |
return db.allDocs({ include_docs: true }).then(function (doc) { | |
console.log(doc); | |
}); | |
} | |
saveTask().then(function () { | |
purge().then(function () { | |
printAllDocs(); // contains 1 doc, but shouldn't it be empty? | |
setTimeout(function () { | |
printAllDocs(); // is empty as expected | |
}, 2000); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment