Last active
August 29, 2015 14:25
-
-
Save stevehobbsdev/7c01468e3d35c91ef299 to your computer and use it in GitHub Desktop.
Promisifying Azure Table Storage queries in Node
This file contains hidden or 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
/** | |
* ATS Promisfy node demo | |
* Steve Hobbs, July 2015 | |
* See associated package.json for package install | |
*/ | |
var chalk = require('chalk'); | |
var azure = require('azure-storage'); | |
var deferred = require('deferred'); | |
var config = { | |
accountName: '**', | |
key: '**' | |
}; | |
// Create a promisify method that takes the original ATS method and turns it into | |
// one that returns a promise object. | |
var promisify = function(fn, target) { | |
return function() { | |
var d = deferred(); | |
var args = Array.prototype.slice.call(arguments, 0, arguments.length); | |
args.push(function(err, result) { | |
if (err) | |
d.reject(err); | |
else | |
d.resolve(result); | |
}); | |
fn.apply(target, args); | |
return d.promise; | |
}; | |
}; | |
console.log(chalk.yellow('Loading Azure entities..')); | |
// Create table service | |
var tableService = azure.createTableService(config.accountName, config.key); | |
// Promisify the 'retrieveEntity' method | |
var retrieveEntity = promisify(tableService.retrieveEntity, tableService); | |
// Promisify the 'createTable' method | |
var createTable = promisify(tableService.createTableIfNotExists, tableService); | |
// Use the promisified methods | |
retrieveEntity('posts', 'general', 'AAAA7216583744299')(function(result) { | |
console.log(result); | |
}); | |
createTable('testTable').done(function(result) { | |
console.log('Table created') | |
}); |
This file contains hidden or 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
{ | |
"name": "azure-promisify", | |
"version": "1.0.0", | |
"description": "", | |
"main": "app.js", | |
"scripts": { | |
"start": "node app.js" | |
}, | |
"author": "Steve Hobbs <[email protected]> (http://stevescodingblog.co.uk)", | |
"license": "ISC", | |
"dependencies": { | |
"azure": "^0.10.6", | |
"azure-storage": "^0.4.5", | |
"chalk": "^1.0.0", | |
"deferred": "^0.7.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment