GithubHelp home page GithubHelp logo

Comments (2)

jasonkuhrt avatar jasonkuhrt commented on June 23, 2024

Thinking about if hooks can return result types. This would facilitate the flexibility for extensions to retry hooks arbitrarily. It would also permit other use-cases than retry, not specific to it.

// ...
let result = await exchange({....})
let i=0
while (result instanceof Error) {
  if (i > 3) return result // error
  i++
  result = await exchange({....})
}
const { unpack } = result
// ...

To opt out of error error handling users could use .throw() on the executed hook:

// ...
const { unpack } = await exchange({....}).orThrow()
// ...

When an extension would re-run a hook it would mean that all the extensions lower in the stack would execute again as well.

from graphql-request.

jasonkuhrt avatar jasonkuhrt commented on June 23, 2024

After banging my head on this a few days I think I've realized what I want to do is impossible.

await run(
      async function foo({ a }) {
        const resultA1 = await a()
        const resultB1 = await resultA1.b() // imagine this is an error
        const resultB2 = await resultA1.b()
        return resultB2
      },
      async function bar({ a }) {
        const resultA1 = await a()
        const resultB1 = await resultA1.b()
        if (resultB1 instanceof Error) throw resultB1
        return resultB1
      },
    )

If extension foo does a retry on hook b there is no way for extension bar to then resume from await resultA1.b() which is what so far the ideas in this issue would assume would happen. Well... no it is not possible to "rewind" the function body like that.

The inability to retry a hook is a DX miss here... had the hooks been separate functions it would have been possible to add retry to any of them. The single function body approach seems to give up that potential.

If the extension is the last in the stack, it actually can retry because there is no rewinding of downstream functions required. What could a configuration API make that feature obvious and safe though?

Graffle
	.create()
	.extend(...)
	.extend(...)
	.extend({
	  anyware: async (...) => { ... }, 
      anywareCatch: async ({ exchange }) => {
        let result = await exchange()
        if (result instance Error) result = ...
      }
    })

The typing for a catch extension will need to differ. Its hooks will return an either type that exposes the error for programatic control.

How would this look in Anyware? Just a new catch parameter I think...

anyware.run({
  initialInput,
  extensions,
  catch,
})

from graphql-request.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.