开发者

CoffeeScript to Node.js Syntax problem of ")"

开发者 https://www.devze.com 2023-03-31 05:28 出处:网络
I have a problem \"too many ) on line 开发者_运维百科21\" I can\'t compile this Coffee Script Code into Node.js

I have a problem "too many ) on line 开发者_运维百科21" I can't compile this Coffee Script Code into Node.js Any help about these ")" ? Thanks

amqp = require('amqp')

class Queue
    constructor: (ip = 'localhost') ->
            @ip = ip
            @receivedObject
            @connection = amqp.createConnection({ host: @ip })

    subscribeTaskQueue: (queueToSubscribe) ->
            self = @
            self.connection.on('ready', ->
                    q = self.connection.queue(queueToSubscribe)
                    q.bind('#')

                    q.subscribe({ ack: true }, (message) ->
                            self.receivedObject = message
                            console.log(self.receivedObject)
                    )
            )

    addTaskToQueue: (queue, objectToSend) ->
            @connection.publish(queue, objectToSend)

module.exports = Queue


This is one area in which CoffeeScript rocks: you can omit the parens when calling functions with arguments! This is especially nice in Node, where the callback is usually the last argument.

Like this:

amqp = require 'amqp'

class Queue

   constructor: (ip = 'localhost') ->
        @ip = ip
        @receivedObject
        @connection = amqp.createConnection { host: @ip } 

   subscribeTaskQueue: (queueToSubscribe) ->
        self = @
        self.connection.on 'ready', ->
                q = self.connection.queue queueToSubscribe
                q.bind '#'

                q.subscribe { ack: true }, (message) ->
                        self.receivedObject = message
                        console.log(self.receivedObject)

   addTaskToQueue: (queue, objectToSend) ->
        @connection.publish queue, objectToSend

module.exports = Queue 
0

精彩评论

暂无评论...
验证码 换一张
取 消