Created
March 26, 2015 21:40
-
-
Save mckoss/a56114f27c4a90f51502 to your computer and use it in GitHub Desktop.
Firebase Promise Wrappers
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
// 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