Promise / async/await support
See original GitHub issueHi,
Thanks for this library!
I use koa@2 that supports async/await syntax. My use case is:
const forms = require('forms')
let myForm = forms.create({
username: fields.string({ required: true })
})
// POST request handler
app.use(async function(ctx) {
let boundedForm = myForm.bind(ctx.request.body)
let errors = await boundedForm.validate() // <-- I need syntax like this
if (errors) {
ctx.render('index', {
form: boundedForm.toHTML()
})
} else {
ctx.body = 'form is valid'
}
})
What do you think about support of promises (promisification)?
Currently, I forced to use my wrapper around this library. It promisifies validate
function:
const forms = require('forms')
let {create} = forms
forms.create = function (fields, options) {
let form = create(fields, options)
let {bind} = form
form.bind = function (data) {
let boundedForm = bind(data)
// @see https://github.com/caolan/forms/blob/v1.3.0/lib/forms.js#L45
let {validate} = boundedForm
boundedForm.validate = function () {
let deferred = new Deferred()
validate(deferred.resolve)
return deferred.promise
}
return boundedForm
}
return form
}
class Deferred {
constructor() {
this.promise = new Promise(this.onPromise.bind(this))
}
onPromise(resolve, reject) {
this.resolve = resolve
this.reject = reject
}
resolve() {}
reject() {}
}
module.exports = forms
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:12 (6 by maintainers)
Top Results From Across the Web
async function - JavaScript - MDN Web Docs - Mozilla
Async functions can contain zero or more await expressions. Await expressions make promise-returning functions behave as though they're ...
Read more >Async functions | Can I use... Support tables for HTML5, CSS3 ...
Async functions make it possible to treat functions returning Promise objects as if they were synchronous. Usage % of. all users, all tracked,...
Read more >Promises, async/await - The Modern JavaScript Tutorial
Promises, async/await · Introduction: callbacks · Promise · Promises chaining · Error handling with promises · Promise API · Promisification · Microtasks ·...
Read more >Async functions: making promises friendly - web.dev
If you use the async keyword before a function definition, you can then use await within the function. When you await a promise, ......
Read more >Difference between promise and async await in Node.js
Async /Await is used to work with promises in asynchronous functions. It is basically syntactic sugar for promises. It is just a wrapper...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@voxpelli regarding the github org request: if @ljharb would like me to move the repository to an organisation, or simply add new collaborators here, I’m happy to do it. I’d rather not have the complexity of a new organisation for a small module myself, but I’ll follow @ljharb’s lead.
If it’s reachable, it’s public ¯\_(ツ)_/¯ certainly a maintainer could choose to be capricious and break someone knowingly, but i certainly wouldn’t do that.