简体   繁体   中英

How to properly return defered object in iced coffeescript

I'm trying to do async/await style of programming using coffeescript and iced coffeescript.

What I get instead if 'undefined' response.

module.exports = update: (req, res) ->
    await user = User.find({ id: 1 }).exec (err, user) ->
        throw err if err?

        console.log(err) # null
        console.log(user) # object

        defer user

    console.log user # undefined

Your approach will not work because the iced-coffeescript syntax for await, defer is used slightly differently. See #iced

You have put defer into the await code block, but it is actually used a as callback for await .

Edit: Callback function was missing.

module.exports = update: (req, res) ->

  findUser = (id, cb) ->
    await user = User.find({ id: 1 }), defer err, user
    if err then return [ err, null ]
    cb err, user

  await findUser id, defer result

  console.log result.user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM