What's New

New Node.js 8 runtime for IBM Cloud Functions

Share this post:

Introducing a new runtime for JavaScript IBM Cloud Functions based on Node.js version 8. Now you can take advantage of the new features that are offered by the recently released Node.js version 8 on Long Term Support (LTS).

This new runtime comes with pre-installed npm packages (bm_db, ibm-cos-sdk, cloudant, watson-developer-cloud) that are useful when integrating serverless functions with IBM Cloud services. For more details on the Node.js 8 environment, check the documentation.

What’s new in Node.js version 8

The next major release of Node.js brings a range or significant changes and additions for JavaScript users. This article highlights the most significant changes and features.

With version 6 of Node.js, you are taking advantage of ES2015 language specification, such as Object destructuring. Now with version 8 of Node.js, you can leverage more language features in your IBM Functions, like JavaScript ES2017 async functions, which makes it easier to write code that can use and create Promises.

Using async functions, waiting for a Promise to resolve is as simple as typing await before it, and proceeding as if the value were synchronously available (without using callbacks).

Example

In this easy to follow example, you can see how to use async and await with IBM Db2 Warehouse on Cloud service.

1. Create a JavaScript file “Employees.js” with the following code:

[code language=”javascript”]
const ibmdb = require(‘ibm_db’)
const queryString = ‘SELECT FIRST_NAME, LAST_NAME, EMAIL from GOSALESHR.employee FETCH FIRST 3 ROWS ONLY’
async function main ({
__bx_creds: { dashDB: { ssldsn } }
}) {
let connection, result, rows
try {
connection = await ibmdb.open(ssldsn)
rows = await connection.query(queryString)
connection.close()
result = { employees: rows }
} catch (err) {
console.error(err.message)
result = { message: err.message }
}
return result
};
[/code]

2. Deploy your function by using the CLI, and give it the name “Employees”.

bx wsk action update Employees Employees.js --kind nodejs:8

Important: When you create or update an Action, always specify the kind nodejs:8. If the kind is not explicitly provided, the action uses the default nodejs:6 version.

3. Bind the IBM Cloud service to your function to set the ssldsn as a default parameter.

[code language=”bash”]
bx wsk service bind dashDB Employees
[/code]

For more information about the new service bind for IBM Cloud Functions, see the following blog post.

If you don’t yet have the service created in your Namespace, use the following command to create the service instance.

[code language=”bash”]
bx service create dashDB Entry db2
[/code]

4. Invoke the Action from the CLI for testing, and retrieve the results:

[code language=”bash”]
bx wsk action invoke Employees -r
[/code]

[code language=”javascript”]
{
"employees": [
{
"EMAIL": "DPage@grtd123.com",
"FIRST_NAME": "Denis",
"LAST_NAME": "Pagé"
},
{
"EMAIL": "EMichel@grtd123.com",
"FIRST_NAME": "Élizabeth",
"LAST_NAME": "Michel"
},
{
"EMAIL": "EClermont@grtd123.com",
"FIRST_NAME": "Émile",
"LAST_NAME": "Clermont"
}
]
}
[/code]

 

Get started with IBM Cloud Functions with your Free account.

Senior Technical Staff Member (STSM) - IBM Cloud Functions

More What's New stories
August 31, 2018

What’s Included in the IBM Cloud Developer Tools CLI Version 2.1.4

This new release of IBM Cloud Developer Tools CLI includes a number of usability improvements and features, such as the ability to add your previously created app to an IBM Cloud Toolchain with the edit command.

Continue reading

August 27, 2018

Newly Enhanced Cloud Partner Incubator Program Announced for IBM Solution Providers

We have custom-tailored the IBM Cloud Solutions Partner Incubator program to help ISVs, MSPs, and other companies developing their own IP fully leverage the power, depth, and experience of IBM to speed time to market and reduce time to revenue.

Continue reading

August 16, 2018

Call for Code is Ready to Help Save Lives When Weather Strikes

Perhaps the next great advancement of our age, a better way to protect people from destruction, is an idea you have . . . a code that can save lives. Learn more about Call for Code, a rallying cry for developers to help create the next big solution to disaster crisis.

Continue reading