From 9c6d1e08e2c76d52575d77509fbc00123139d28e Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Fri, 21 Oct 2016 22:42:27 +0200 Subject: [PATCH 1/3] add promised land function to make it easy to promisify all cradle objects --- lib/cradle/promised-land.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lib/cradle/promised-land.js diff --git a/lib/cradle/promised-land.js b/lib/cradle/promised-land.js new file mode 100644 index 0000000..095d5aa --- /dev/null +++ b/lib/cradle/promised-land.js @@ -0,0 +1,22 @@ +'use strict'; + +function promisify(options, key) { + let obj = options[key]; + return obj ? promisifyAll(obj) : undefined; +} + +module.exports = function(promisifyAll, options) { + ['cradle', 'client', 'db'].reduce(function(key, result) { + result[key] = promisify(options, key); + return result + }, {}) +} + +// Usage +var P = require('bluebird') +const promisedLand = require('cradle/promised-land'); +const promised = promisedLand(P.promisifyAll, {cradle, client, db}); + +// promised.db +// promised.client +// promised.cradle From 4f933f06a88025673f81c26de67c3e0ea7191cab Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Fri, 21 Oct 2016 22:56:03 +0200 Subject: [PATCH 2/3] add Async API notes to Readme --- README.md | 22 ++++++++++++++++++++++ lib/cradle/promised-land.js | 8 -------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 31269bf..f56f4b5 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,28 @@ You can also get the current revisions limit }); ``` +Modern async APIs +--- + +If you prefer to use modern async Javascript with `Promise`s or `async/await`, you can use the new handy `promised-land` helper method as follows: + +``` +const cradle = require('cradle'); +const client = // configure client +const db = // configure db from client + +const P = require('bluebird') +const promisedLand = require('cradle/promised-land'); +const promised = promisedLand(P.promisifyAll, {cradle, client, db}); +``` + +Note that you can pass in whatever method you prefer to wrap all Object functions in promises. Here we use the `promisifyAll` method from the [bluebird](https://github.com/petkaantonov/bluebird) promise library. + +You can now use the following promised enabled APIs as you see fit, even with `async/await` :) + +- `promised.db` +- `promised.client` +- `promised.cradle` ### fetching a document _(GET)_ ### diff --git a/lib/cradle/promised-land.js b/lib/cradle/promised-land.js index 095d5aa..be28e30 100644 --- a/lib/cradle/promised-land.js +++ b/lib/cradle/promised-land.js @@ -12,11 +12,3 @@ module.exports = function(promisifyAll, options) { }, {}) } -// Usage -var P = require('bluebird') -const promisedLand = require('cradle/promised-land'); -const promised = promisedLand(P.promisifyAll, {cradle, client, db}); - -// promised.db -// promised.client -// promised.cradle From 84dafe69348524bfd0830ed7da34c77cc9828e57 Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Fri, 21 Oct 2016 23:00:43 +0200 Subject: [PATCH 3/3] add js marker to promised land code example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f56f4b5..bc8655c 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ Modern async APIs If you prefer to use modern async Javascript with `Promise`s or `async/await`, you can use the new handy `promised-land` helper method as follows: -``` +```js const cradle = require('cradle'); const client = // configure client const db = // configure db from client