Skip to content

Alternative API Proposal #105

@martinheidegger

Description

@martinheidegger

With two variables to set at the same time the code seems to easily become very unwieldy.

const a = AsyncContext.Variable()
const b = AsyncContext.Variable()

a.run('x', async () => {
   b.run('y', async () => {
      console.log([a.get(), b.get()])
   }
})

in Python if you use a context, you can specify more than one value at a time. However, since the run function is defined on the Variable, there is no easy way to work around this.

I am wondering if a different, inverted API couldn't prove both more flexible and better readable?

const a = Symbol('a')
const b = Symbol('b')
const { run, get } = AsyncContext

run({ [a]: 'x', [b]: 'y' }, async () => {
    console.log([get(a), get(b)])
})

Note: this proposal could possible address the issue in #76 by allowing the internals to use a Object instead of a Map to store.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions