harcon icon indicating copy to clipboard operation
harcon copied to clipboard

code from quick-setup chapter seems broken

Open xrogtolis opened this issue 8 years ago • 9 comments

I am trying to run(with node v8.5.0) the quick-setup and I get the following error:

index.js:3
let inflicter = await harcon.init()
                      ^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:201:16)
    at bootstrap_node.js:626:3

xrogtolis avatar Sep 14 '17 20:09 xrogtolis

Hello, maybe the first 2 lines are missing?

var Harcon = require('harcon')
let harcon = new Harcon( { /* opts */ } )

Could you share the complete code of yours?

imrefazekas avatar Sep 14 '17 20:09 imrefazekas

this is my index.js:

var Harcon = require('harcon')
let harcon = new Harcon( { /* opts */ } )
let inflicter = await harcon.init()


// define a listener function listening every message related to "greet" like "greet.goodmorning" or "greet.goodday"
await inflicter.addict( null, 'peter', 'greet.*', async function (greetings1, greetings2) {
	return 'Hi there!'
} )

// define an plain object serving as listener withing the context "greet" to messages "warm"
marie = {
	name: 'marie',
	context: 'greet',
	warm: async function (greetings1, greetings2) {
		return 'Bonjour!'
	}
}
await inflicter.addicts( marie )

// sends a communication 'greet.everyone' with parameters and waits for responses
// will receive back 2 answers: 'Hi there!' and 'Bonjour!'
await harcon.simpleIgnite( 'greet.everyone', 'Whatsup?', 'How do you do?' )

and my package.json:

{
  "name": "mss",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "harcon": "^8.1.8"
  }
}

xrogtolis avatar Sep 14 '17 20:09 xrogtolis

I guess, the reason you receive this is because await can not stand at the top level of a module. You might need to put it into a function like this:

var Harcon = require('harcon')
let harcon = new Harcon( { /* opts */ } )

async function init () {
	let inflicter = await harcon.init()

	await inflicter.addict( null, 'peter', 'greet.*', async function (greetings1, greetings2) {
		return 'Hi there!'
	} )

	let marie = {
		name: 'marie',
		context: 'greet',
		warm: async function (greetings1, greetings2) {
			return 'Bonjour!'
		}
	}
	await inflicter.addicts( marie )

	await harcon.simpleIgnite( 'greet.everyone', 'Whatsup?', 'How do you do?' )
}

init().then( () => {
	// ...
} ).catch( () => {
	// ...
} )

imrefazekas avatar Sep 14 '17 20:09 imrefazekas

As reference, please check this: https://news.ycombinator.com/item?id=12497114

imrefazekas avatar Sep 14 '17 20:09 imrefazekas

Thank you, it makes sense now. I am still new to async/await

xrogtolis avatar Sep 14 '17 20:09 xrogtolis

No worries, let me know if I can assist you in anything. :)

imrefazekas avatar Sep 14 '17 20:09 imrefazekas

An examples folder would be very helpful :)

xrogtolis avatar Sep 14 '17 21:09 xrogtolis

I considered the mocha tests as examples but you are right, I will create examples tomorrow afternoon (GMT) Would that be convenient for you?

imrefazekas avatar Sep 14 '17 21:09 imrefazekas

Of course, no rush :) I'll start by reading the tests in the meantime

xrogtolis avatar Sep 14 '17 21:09 xrogtolis