Created
October 21, 2019 22:45
-
-
Save rshaker/63496307d9bbd390d91651fc9801ad6d 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
const nano = require('nano')('http://localhost:5984'); | |
(async function gobbler() { | |
//Async-await pattern: | |
const myfunc = async function asyncCall() { | |
try { | |
await nano.db.destroy('alice'); | |
} | |
catch(e) { | |
console.log(e); // triggers first run | |
} | |
await nano.db.create('alice'); | |
const alice = nano.use('alice'); | |
const response = await alice.insert({ happy: true }, 'rabbit'); | |
return response; | |
}; | |
await myfunc(); | |
//Promise pattern: | |
await nano.db.destroy('alice') | |
.catch((error) => { // shouldn't trigger... | |
console.log(error); | |
}) | |
.then((result) => { | |
console.log(result); | |
return nano.db.create('alice'); | |
}) | |
.then((result) => { | |
console.log(result); | |
const alice = nano.use('alice'); | |
return alice.insert({ happy: true }, 'rabbit'); | |
}) | |
.then((result) => { | |
console.log(result); | |
}) | |
.catch((error) => { | |
console.log(error); | |
}) | |
.finally(() => { | |
console.log("all done!"); | |
}); | |
//Abbreviated promise: | |
await nano.db.destroy('alice').catch(() => { }) | |
.then(() => nano.db.create('alice')) | |
.then(() => { return nano.use('alice') }) | |
.then((useResult) => useResult.insert({ happy: true }, 'rabbit')); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment