February 14, 2018 By Carlos Santana 2 min read

New Node.js 8 runtime for IBM Cloud Functions

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:

<br>
   const ibmdb = require('ibm_db')<br>
   const queryString = 'SELECT FIRST_NAME, LAST_NAME, EMAIL from GOSALESHR.employee FETCH FIRST 3 ROWS ONLY'<br>
   async function main ({<br>
   __bx_creds: { dashDB: { ssldsn } }<br>
   }) {<br>
     let connection, result, rows<br>
     try {<br>
      connection = await ibmdb.open(ssldsn)<br>
      rows = await connection.query(queryString)<br>
      connection.close()<br>
      result = { employees: rows }<br>
    } catch (err) {<br>
      console.error(err.message)<br>
      result = { message: err.message }<br>
    }<br>
    return result<br>
   };<br>

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.

<br>
bx wsk service bind dashDB Employees<br>

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.

<br>
bx service create dashDB Entry db2<br>

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

<br>
bx wsk action invoke Employees -r<br>
<br>
  {<br>
    "employees": [<br>
      {<br>
        "EMAIL": "DPage@grtd123.com",<br>
        "FIRST_NAME": "Denis",<br>
        "LAST_NAME": "Pagé"<br>
      },<br>
      {<br>
        "EMAIL": "EMichel@grtd123.com",<br>
        "FIRST_NAME": "Élizabeth",<br>
        "LAST_NAME": "Michel"<br>
      },<br>
      {<br>
        "EMAIL": "EClermont@grtd123.com",<br>
        "FIRST_NAME": "Émile",<br>
        "LAST_NAME": "Clermont"<br>
      }<br>
    ]<br>
  }<br>

Get started with IBM Cloud Functions with your Free account.

More from

Advance your enterprise Journey to Hybrid Cloud and AI powered by AIOps on Z

2 min read - Thanks to rising costs, skills shortages and ever-growing security threats, businesses must adapt quickly to shifts in demand patterns brought on by a digital workforce and rapidly changing buyer behavior. That requires putting extra emphasis on the resiliency and performance of your business processes and supporting applications. For larger IT organizations with increasingly hybrid and complex application landscapes that often include IBM Z®, it’s essential to take a comprehensive approach to IT operations. The challenge becomes “How do you effectively sift through terabytes of…

IBM API Connect named a leader in the Forrester Wave: API Management Software, Q3 2024

4 min read - We are excited to announce that Forrester has recognized IBM API Connect® as a Leader in The Forrester Wave™: API Management Software, Q3 20241. Forrester conducted a 24-criteria evaluation of API management software providers to make their assessment and final results. IBM API Connect received the highest score possible in 17 out of the 24 criteria. [button link="https://www.ibm.com/account/reg/us-en/signup?formid=urx-52934"]Download a complimentary copy of the report here[/button] IBM: What to look for when shopping for API Management Software Transformation and integration initiatives…

Success and recognition of IBM offerings in G2 Summer Reports  

2 min read - IBM offerings were featured in over 1,365 unique G2 reports, earning over 230 Leader badges across various categories.   This recognition is important to showcase our leading products and also to provide the unbiased validation our buyers seek. According to the 2024 G2 Software Buyer Behavior Report, “When researching software, buyers are most likely to trust information from people with similar roles and challenges, and they value transparency above other factors.”  With over 90 million visitors each year and hosting more than 2.6…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters