tinovyatkin / pass-js

Apple Wallet Passes generating library for Node.JS
MIT License
691 stars 82 forks source link

How to use async/await with node 8 #3

Closed sja closed 7 years ago

sja commented 7 years ago

I'm using NodeJS 8.3.0 and I get the following error:

/Users/sja/.nvm/versions/node/v8.3.0/bin/node /Users/sja/dev/mypass/index.js
/Users/sja/dev/mypass/index.js:12
await template.images.loadFromDirectory("./images");
      ^^^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:537:28)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:578:3

Process finished with exit code 1
tinovyatkin commented 7 years ago

You can use await only inside an async function. Are you doing so?

Basically, it's like this

const { Template } = require('@destinationstransfers/passkit');
async function initTemplate() {
  const template = new Template('boardingPass');
  await template.images.loadFromDirectory('imagesDirectory');
}

// somewhere later you call the initTemplate or do something like this:
setImmediate(initTemplate);

I do recommend consider Template.load - that loads images and all template fields from pass.json. We will support localizations load soon too.

sja commented 7 years ago

Ah, great! Thanks. I always used Kris Kowalls Q, not the "native" promises. And IntelliJ had no helpful hint on that: Expression statement is not assignment or call

Thanks for the fast reply!