Giter Club home page Giter Club logo

Comments (3)

filmaj avatar filmaj commented on August 16, 2024 1

thx bri!

at the expense of needing to understand how functions work

😆

from functions.

brianleroux avatar brianleroux commented on August 16, 2024

np! I love questions. the 'secret' is that async functions are just sugar for internal promise handling so as long as you return a Promise your function can be awaited. (any function can be awaited and the runtime will auto wrap a promise which apparently isn't a big deal for performance but I've observed it to be slower. for CLI tools its not a big deal tho.)

we probably won't be adding async functions in the impl code anytime soon. the feature is newer, slower and has undesirable error handling consequences. that being said, userland is embracing async/await in a big way. so we're moving towards a world where our examples and userland code is all async/await but our implementation code supports all three: accepting a node style error first callback, if none is supplied returning a promise and therefor supporting async/await.

callbacks are more performant, natural to the node runtime and have more deterministic error handling semantics at the expense of needing to understand how functions work. promises have been a nightmare on the frontend but are definitely improved, quite recently, with actual line numbers/stack traces in errors. we could move this way but imo that'd be a rewrite for aesthetic reasons.

example of writing a durable function that supports all three asynchronous patterns:

module.exports = function slippery(params, callback) {
  let promise
  if (!callback) {
    promise = new Promise((res, rej)=> {
       callback = function(err, result) {
         err ? rej(err) : res(result)
       }
    })
  }
  callback(null, 'some value') // this could be within other callback-y code
  return promise
}

which is the the boilerplate I'm now using for anything userland (like session)… thx tc39. this pattern is extremely flexible though…

you can use callbacks:

let slippery = require('./slippery')
slippery(console.log) // error is bubbled and can be handled or thrown

or if you like typing you can use promises for the equiv code:

let slippery = require('./slippery')
slippery().then(console.log).catch(console.log) // don't forget catch!

and the oft advertised as the least verbose but in practice it def is not async/await:

let slippery = require('./slippery')
async function ugh() {
  try {
    console.log(await slippery())
  }
  catch(e) {
    console.log(e)
  }
}
ugh()

so that brings us back to session. the interface for session is the same pattern as above https://github.com/arc-repos/arc-functions/blob/master/src/http/providers/ddb.js#L14 so you don't strictly need to be using async/await. you could opt into either callback code or promise based code if you should wish.

hope this helps! happy to follow on any other questions.

from functions.

brianleroux avatar brianleroux commented on August 16, 2024

glad you picked up on my snark ;) …updated the async example at the end to be semantic equiv the the others.

from functions.

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.