Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion functions/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ exports.helloWorld = function helloWorld (context, data) {
var request = require('request-promise');

/**
* Background Cloud Function that returns a Promise.
* Background Cloud Function that returns a Promise. Note that we don't pass
* a "context" argument to the function.
*
* @param {Object} data Request data, provided by a trigger.
* @returns {Promise}
Expand All @@ -48,3 +49,20 @@ exports.helloPromise = function helloPromise (data) {
});
};
// [END helloPromise]

// [START helloSynchronous]
/**
* Background Cloud Function that returns synchronously. Note that we don't pass
* a "context" argument to the function.
*
* @param {Object} data Request data, provided by a trigger.
*/
exports.helloSynchronous = function helloSynchronous (data) {
// This function returns synchronously
if (data.something === true) {
return 'Something is true!';
} else {
throw new Error('Something was not true!');
}
};
// [END helloSynchronous]
14 changes: 14 additions & 0 deletions test/functions/background.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ test.cb.serial('should make a promise request', function (t) {
t.fail();
});
});
test('should return synchronously', function (t) {
var backgroundSample = getSample();
t.is(backgroundSample.sample.helloSynchronous({
something: true
}), 'Something is true!');
});
test('should throw an error', function (t) {
var backgroundSample = getSample();
t.throws(function () {
backgroundSample.sample.helloSynchronous({
something: false
})
}, Error, 'Something was not true!');
});

test.after(function () {
console.error.restore();
Expand Down