0

I have written code for newer Node version which I wish to implement as Google Cloud Function (GCF). Now issue is that GCF only offers Node v6.14.0 engine which does not support Async functions.

Solution that I liked was to use https://www.npmjs.com/package/asyncawait given that I won't need to do major changes (also same applies when I later wish to use the native async once newer Node is available). However, I am unable to run my code even though I believe I have followed the examples in my conversions correctly.

I either get error "xx is not a function" if I declare the function as per the instructions or if I declare the function differently (which I think should work) I get "xx is not defined".

Below are two play snippets that produce the errors. Number 1) is as per instructions in the npm website and number 2) the alternative.

Number 1) as per the instructions -> "printStuff is not a function" error

var async = require('asyncawait/async');
var await = require('asyncawait/await');

printStuff()

var printStuff = async (function () {
    var files = await (console.log("x"));
    var files2 = await (console.log("y"));
    return
});

Number 2) the alternative -> "printStuff is not defined" error (I have also tried adding more brackets to be in line with the example here - half way down the page which uses the same package - SyntaxError: Unexpected token function - Async Await Nodejs)

var async = require('asyncawait/async');
var await = require('asyncawait/await');

printStuff();

async (function printStuff() {
    var files = await (console.log("x"));
    var files2 = await (console.log("y"));
    return
});

Any help would be appreciated!

Thank you!!

Jontsu
  • 63
  • 6

1 Answers1

3

Move function invocation below the definition:

var async = require('asyncawait/async');
var await = require('asyncawait/await');

var printStuff = async (function () {
    var files = await (console.log("x"));
    var files2 = await (console.log("y"));
    return [files, files2];
});

printStuff();

https://runkit.com/5b1e296261083b0012f4d373/5b461db6a6a0a90012a344cc

Pavlo
  • 43,301
  • 14
  • 77
  • 113
  • Thank you!! Wish I would have asked earlier, wasted so much time on that :D Did not expect that to make a difference since it did not make a difference with the code that I ran at the later Node version. Tried to upvote, but don't have rep to make a difference yet ;) – Jontsu Jul 11 '18 at 19:17