Currently the simplest solution is one of three based on personal
preference.

You can use bind to get node style async functions to return a yieldable
thunk

This is based on [gens](https://github.com/Raynos/gens)

```js
// update(key, value, callback)
var update = async(function* update(key, value) {
    var current = yield db.get.bind(null, key)

    for (var key in value) {
        current[key] = value[key]
    }

    yield db.set.bind(null, key, current)

    return current
})
```

You can have the async helper pass a resumer as the first argument.

This is based on [suspend](https://github.com/jmar777/suspend)

```js
// update(key, value, callback)
var update = async(function* update(resume, key, value) {
    var current = yield db.get(key, resume)

    for (var key in value) {
        current[key] = value[key]
    }

    yield db.set(key, current, resume)

    return current
})
```

You can also define your functions using just * & yield. This means that
non generator calls
would have to use some kind of `invoke` function.

For this to get maximum benefit you should be able to do `yield fn(...)`
where fn is another generator.

This is like [galaxy](https://github.com/bjouhier/galaxy)

```js
// invoke(update, key, value, callback)
var update = function* update(key, value) {
    var current = yield db.get.bind(null, key)

    for (var key in value) {
        current[key] = value[key]
    }

    yield db.set.bind(null, key, current)

    return current
}
```

If ES7 implements async/await AND they support both promises & thunks then
in the future you can do

```js
// ES7 update(key, value, callback)
var update = async function update(key, value) {
    var current = await db.get.bind(null, key)

    for (var key in value) {
        current[key] = value[key]
    }

    await db.set.bind(null, key, current)

    return current
}
```


On Fri, Aug 16, 2013 at 3:09 PM, Mark Hahn <[email protected]> wrote:

> I've researched the various solutions to make generators work with or
> replace node callbacks.  It appears that they fall into two camps.  In one
> you wrap functions to make them callable as sync and in the other camp you
> write sync code inside a generator.  Is this correct?  I know it is an
> over-simplification.
>
> They both seem like a bit of a pain to use.  There is a lot of new
> semantics to master.  Is there any possibility a simpler solution could
> exist in the future?
>
>
>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to