diff --git a/functions/background/index.js b/functions/background/index.js index 767570eb2e..9f65e6f788 100644 --- a/functions/background/index.js +++ b/functions/background/index.js @@ -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} @@ -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] diff --git a/test/functions/background.test.js b/test/functions/background.test.js index 64b925ca2d..ae12844a26 100644 --- a/test/functions/background.test.js +++ b/test/functions/background.test.js @@ -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();