Create a gist now

Instantly share code, notes, and snippets.

Using the Q promise library with Parse.com Cloud Code to ease offline development and testing.
/*globals Parse*/
/**
* Playing with using the Q promise library with Parse.com
* to ease offline development and testing.
*
* http://documentup.com/kriskowal/q/
*
* Was tricky to mock-up Parse's promise implementation.
*
* Author: Brian Gershon <brian.five@gmail.com>
*/
var q = require('cloud/q.js');
function doSimpleStuff() {
//return q.reject('ERR ERR');
return q('simple hi');
}
function doMoreExoticStuff() {
return q.fcall(function () {
return 'exotic hi';
});
}
function doCrazyStuff() {
var defer = q.defer();
defer.promise.then(function success(message) {
console.log('About to say hello. Message: ' + message);
//return 'bye';
}, function error(reason) {
throw new Error(reason);
//return 'error ' + reason;
});
// play with resolving or rejecting this promise
defer.resolve('resolve me');
// defer.resolve('toast');
return defer.promise;
}
Parse.Cloud.define("hello", function (request, response) {
doSimpleStuff()
.then(doMoreExoticStuff)
.then(doCrazyStuff)
.then(
function success(value) {
response.success("Hello world SUCCESS! " + value);
},
function error(reason) {
response.error('Hello world FAILURE ' + reason);
}
)
.fin(function () {
console.log('FINALLY!');
})
.done();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment