Skip to content

Instantly share code, notes, and snippets.

@mckoss
Created March 26, 2015 21:40
Show Gist options
  • Save mckoss/a56114f27c4a90f51502 to your computer and use it in GitHub Desktop.
Save mckoss/a56114f27c4a90f51502 to your computer and use it in GitHub Desktop.
Firebase Promise Wrappers
// Get Firebase value from a query as a Promise.
function getValue(ref, allowNull) {
return new Promise(function(resolve, reject) {
ref.once('value',
function(snap) {
var result = snap.val();
if (result === null && !allowNull) {
reject(new Error("No data at location: " + ref.toString()));
return;
}
resolve(result);
},
function(error) {
reject(new Error(error.toString()));
});
});
}
function setValue(ref, value) {
return new Promise(function(resolve, reject) {
ref.set(value, function(error) {
if (error === null) {
resolve(true);
} else {
reject(new Error(error.toString()));
}
});
});
}
function pushValue(ref, value) {
return new Promise(function(resolve, reject) {
var pushRef = ref.push(value, function(error) {
if (error === null) {
resolve(pushRef);
} else {
reject(new Error(error.toString()));
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment