Skip to content

Commit d8b7857

Browse files
committed
Added promise sample.
1 parent fe684ab commit d8b7857

File tree

5 files changed

+68
-36
lines changed

5 files changed

+68
-36
lines changed
File renamed without changes.

functions/background/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START helloworld]
17+
/**
18+
* Background Cloud Function.
19+
*
20+
* @param {Object} context Cloud Function context.
21+
* @param {Object} data Request data, provided by a trigger.
22+
*/
23+
exports.helloworld = function helloworld (context, data) {
24+
if (data.message === undefined) {
25+
// This is an error case, "message" is required
26+
context.failure('No message defined!');
27+
} else {
28+
// Everything is ok
29+
console.log(data.message);
30+
context.success();
31+
}
32+
};
33+
// [END helloworld]
34+
35+
// [START helloPromise]
36+
var request = require('request-promise');
37+
38+
/**
39+
* Background Cloud Function that returns a Promise.
40+
*
41+
* @param {Object} data Request data, provided by a trigger.
42+
* @returns {Promise}
43+
*/
44+
exports.helloPromise = function helloPromise (data) {
45+
return request({
46+
uri: data.endpoint
47+
});
48+
};
49+
// [END helloPromise]

functions/background/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "nodejs-docs-samples-functions",
3+
"description": "Node.js samples found on https://cloud.google.com",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
11+
},
12+
"dependencies": {
13+
"request-promise": "^3.0.0"
14+
}
15+
}

functions/message/index.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

functions/pubsub/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var pubsub = gcloud.pubsub();
3131
* @param {string} data.topic Topic name on which to publish.
3232
* @param {string} data.message Message to publish.
3333
*/
34-
function publish (context, data) {
34+
exports.publish = function publish (context, data) {
3535
try {
3636
if (!data.topic) {
3737
throw new Error('Topic not provided. Make sure you have a ' +
@@ -63,7 +63,7 @@ function publish (context, data) {
6363
console.error(err);
6464
return context.failure(err.message);
6565
}
66-
}
66+
};
6767

6868
/**
6969
* Triggered from a message on a Pub/Sub topic.
@@ -74,13 +74,10 @@ function publish (context, data) {
7474
* @param {Object} data Request data, in this case an object provided by the Pub/Sub trigger.
7575
* @param {Object} data.message Message that was published via Pub/Sub.
7676
*/
77-
function subscribe (context, data) {
77+
exports.subscribe = function subscribe (context, data) {
7878
// We're just going to log the message to prove that it worked!
7979
console.log(data.message);
8080

8181
// Don't forget to call success!
8282
context.success();
83-
}
84-
85-
exports.publish = publish;
86-
exports.subscribe = subscribe;
83+
};

0 commit comments

Comments
 (0)