Comments (3)
thx bri!
at the expense of needing to understand how functions work
😆
from functions.
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 await
ed. (any function can be await
ed 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.
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)
- Rate exceeded doing SNS scans in events.publish
- arc.http.express does not correctly resolve the request method HOT 1
- arc.tables indexes HOT 2
- Changing the value of NODE_ENV can break builds HOT 4
- Deploy failed after following quickstart HOT 5
- Feature suggestion for full static manifest output HOT 2
- Improve handling of body parsing errors
- Before erroring out due to a missing entry in the service map, run service discovery once
- Publish to sandbox queue reads events port from wrong property HOT 2
- brotli compression breaks Sandbox livereload HOT 1
- dtslint it pinned, either rework TS lint step or scrap it
- A couple type defs missing for aliases HOT 1
- Arc deploy failing with AWS CICD
- Static does not work with fingerprinting HOT 2
- Type definition for tables()._db, tables()._doc is wrong for nodejs18.x runtime HOT 10
- Allow passing translateConfig to Dynamo client and doc client HOT 8
- Add option to enable/disable brotli compression
- JWE secret is not correctly passed for encryption
- arc.http[.async] in version 7.0.0 are overly strict regarding mixing sync and async middleware
- v8@RC arc.tables()['table'].put() returns empty object
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from functions.