Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async function in an Azure Function

I am getting an error with this javascript function definition in Azure. I am defining this within an Azure function for use by the exported function.

async function getData() {
    var data = await postRequest();
    return data;
};

Below is the error I am getting in the console. Is async await syntax not supported in Azure functions?

If yes, how would I implement an async call (such as a REST call made with node-rest-client).

2018-01-05T10:50:55.448 Function started (Id=a4995e6f-141a-46bb-becf-
468f49f7505d)
2018-01-05T10:50:55.565 Exception while executing function: 
Functions.MyFunctionName. mscorlib: One or more errors occurred. 
D:\home\site\wwwroot\MyFunctionName\index.js:90
async function getData() {
      ^^^^^^^^

SyntaxError: Unexpected token function
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at eval (eval at compileFunc (D:\Program Files (x86)\SiteExtensions\Functions\1.0.11388\bin\edge\double_edge.js:34:28), <anonymous>:1:80).
2018-01-05T10:50:55.673 Function completed (Failure, Id=a4995e6f-141a-46bb-becf-468f49f7505d, Duration=221ms)
like image 522
Pradipe Yoggi Avatar asked Jan 05 '18 11:01

Pradipe Yoggi


People also ask

Can an azure function be async?

Prevent problems. The code should be async without any blocking calls is the suggested programming practice for function app. The function apps' runtime allows us to make the call async and returns the result in which we can just 'await' and handle exceptions seamlessly.

What is async function in C#?

An async method runs synchronously until it reaches its first await expression, at which point the method is suspended until the awaited task is complete. In the meantime, control returns to the caller of the method, as the example in the next section shows.

What is async await and how does it work?

The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method.

Why do we use await and async?

An async function is a function declared with the async keyword, and the await keyword is permitted within it. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.


1 Answers

For javascript async syntax support you need to install the latest version of the cli along side with node v8.x

npm i -g azure-functions-core-tools@core

Additionally you'll need to enable the beta runtime on azure so it also works when deploying.

like image 104
nelak Avatar answered Oct 08 '22 16:10

nelak