Skip to content

Commit b02fcb6

Browse files
authored
Add missing sample that got inlined into the docs. (GoogleCloudPlatform#137)
1 parent a6c2db5 commit b02fcb6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

functions/background/index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ exports.helloWorld = function helloWorld (context, data) {
3737
var request = require('request-promise');
3838

3939
/**
40-
* Background Cloud Function that returns a Promise.
40+
* Background Cloud Function that returns a Promise. Note that we don't pass
41+
* a "context" argument to the function.
4142
*
4243
* @param {Object} data Request data, provided by a trigger.
4344
* @returns {Promise}
@@ -48,3 +49,20 @@ exports.helloPromise = function helloPromise (data) {
4849
});
4950
};
5051
// [END helloPromise]
52+
53+
// [START helloSynchronous]
54+
/**
55+
* Background Cloud Function that returns synchronously. Note that we don't pass
56+
* a "context" argument to the function.
57+
*
58+
* @param {Object} data Request data, provided by a trigger.
59+
*/
60+
exports.helloSynchronous = function helloSynchronous (data) {
61+
// This function returns synchronously
62+
if (data.something === true) {
63+
return 'Something is true!';
64+
} else {
65+
throw new Error('Something was not true!');
66+
}
67+
};
68+
// [END helloSynchronous]

test/functions/background.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ test.cb.serial('should make a promise request', function (t) {
7979
t.fail();
8080
});
8181
});
82+
test('should return synchronously', function (t) {
83+
var backgroundSample = getSample();
84+
t.is(backgroundSample.sample.helloSynchronous({
85+
something: true
86+
}), 'Something is true!');
87+
});
88+
test('should throw an error', function (t) {
89+
var backgroundSample = getSample();
90+
t.throws(function () {
91+
backgroundSample.sample.helloSynchronous({
92+
something: false
93+
})
94+
}, Error, 'Something was not true!');
95+
});
8296

8397
test.after(function () {
8498
console.error.restore();

0 commit comments

Comments
 (0)