Skip to content

Commit a3d2dff

Browse files
author
Matt Mason
committed
Added promise sample
1 parent e8f898e commit a3d2dff

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

WebJobs.Script.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25420.1
4+
VisualStudioVersion = 14.0.25123.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebJobs.Script", "src\WebJobs.Script\WebJobs.Script.csproj", "{1DC670CD-F42F-4D8F-97BD-0E1AA8221094}"
77
EndProject
@@ -361,6 +361,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ManualTrigger-CSharp", "Man
361361
sample\ManualTrigger-CSharp\run.csx = sample\ManualTrigger-CSharp\run.csx
362362
EndProjectSection
363363
EndProject
364+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "HttpTrigger-Node-Promise", "HttpTrigger-Node-Promise", "{8E4F0ED5-75E0-4D0C-9469-05F738B1EC38}"
365+
ProjectSection(SolutionItems) = preProject
366+
sample\HttpTrigger-Node-Promise\function.json = sample\HttpTrigger-Node-Promise\function.json
367+
sample\HttpTrigger-Node-Promise\index.js = sample\HttpTrigger-Node-Promise\index.js
368+
EndProjectSection
369+
EndProject
364370
Global
365371
GlobalSection(SolutionConfigurationPlatforms) = preSolution
366372
Debug|Any CPU = Debug|Any CPU
@@ -457,5 +463,6 @@ Global
457463
{94B16BEB-70BD-4357-A21A-D1E70C8CDDF1} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
458464
{C9FCBCFD-0AD1-44B0-A15B-EE2F5219A345} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
459465
{EB3A602B-4B32-4742-B4A2-39445BD2FBE3} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
466+
{8E4F0ED5-75E0-4D0C-9469-05F738B1EC38} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
460467
EndGlobalSection
461468
EndGlobal
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "httpTrigger",
5+
"name": "req",
6+
"direction": "in",
7+
"methods": [ "get" ]
8+
},
9+
{
10+
"type": "http",
11+
"name": "$return",
12+
"direction": "out"
13+
}
14+
]
15+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var http = require('http');
2+
3+
module.exports = function getStockDataFromGoogle(context, req) {
4+
// Get comma-separated list of tickers from body or query
5+
var tickers = req.body.tickers || req.query.tickers;
6+
7+
context.log(`Node.js HTTP trigger function processed a request. Getting ticker prices for ${tickers}`);
8+
9+
// Return a promise instead of calling context.done. As our http response trigger is using the '$return'
10+
// binding, we can directly resolve the promise with our http response object, i.e. { status: ..., body: ... }
11+
return new Promise((resolve, reject) => {
12+
var responseBody = "";
13+
http.get({
14+
host: "www.google.com",
15+
path: `/finance/info?q=${tickers}`
16+
})
17+
.on('response', (resp) => {
18+
// Build the response body as data is recieved via the 'data' event.
19+
// When the 'end' event is recieved, 'resolve' the promise with the built response.
20+
resp.on('data', (chunk) => responseBody += chunk)
21+
.on('end', () => resolve({ status: 200, body: parseGoogleFinance(responseBody) }));
22+
})
23+
// If there is an error, 'resolve' with a 'Bad Request' status code and the error.
24+
// If instead we chose to 'reject' the promise (a good option with unknown/unrecoverable errors),
25+
// the client would receive a 500 error with response body {"Message":"An error has occurred."}.
26+
.on('error', (error) => resolve({ status: 400, body: error }))
27+
.end();
28+
});
29+
30+
function parseGoogleFinance(responseBody) {
31+
// Strip extra characters from response
32+
responseBody = responseBody.replace('//', '');
33+
34+
var data = JSON.parse(responseBody);
35+
36+
return data.reduce((accumulator, stock) => {
37+
// acc[ticker] = price
38+
accumulator[stock.t] = stock.l;
39+
return accumulator;
40+
}, {});
41+
}
42+
}

0 commit comments

Comments
 (0)