File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,8 @@ exports.helloWorld = function helloWorld (context, data) {
3737var 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]
Original file line number Diff line number Diff 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
8397test . after ( function ( ) {
8498 console . error . restore ( ) ;
You can’t perform that action at this time.
0 commit comments