Created
September 28, 2013 23:55
-
-
Save briangershon/6747919 to your computer and use it in GitHub Desktop.
Using the Q promise library with Parse.com Cloud Code to ease offline development and testing.
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
/*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 <[email protected]> | |
*/ | |
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