Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved means to create a Future with no existing async context #20

Open
gwynne opened this issue Dec 19, 2017 · 0 comments
Open

Improved means to create a Future with no existing async context #20

gwynne opened this issue Dec 19, 2017 · 0 comments
Milestone

Comments

@gwynne
Copy link
Member

gwynne commented Dec 19, 2017

JavaScript's Promise library provides Promise.try() as a static method primarily for the purpose of folding error handling of "before the async stuff starts" code into the async model, so that the developer doesn't need to handle both sync and async errors:

return Promise.try(() => {
    // do some sync work that could throw an error
    return someSynchronousValue
}).then((value) => {
    // use the value, do some async work
});

or

return Promise.try(() => {
    // do some sync work that could throw an error
    return startAsyncPromise()
}).then((value) => {
    // use the value
});

These cases correspond neatly to Future's map(to:) and flatMap(to:) methods - I propose adding static variations, like so:

return Future.map(to: SomeType.self) {
    return try getSomeSynchronousValue()
}.flatMap(to: SomethingElse.self) {
    return try getSomethingElseAsync($0)
}.map(to: Response.self) {
    return makeResponseWithValue($0)
}

or

return Future.flatMap(to: SomethingElse.self) {
    return try getSomethingElseAsync()
}.map(to: Response.self) {
    return makeResponseWithValue($0)
}
@tanner0101 tanner0101 added this to the 1.0 milestone Dec 19, 2017
@tanner0101 tanner0101 added this to Backlog in Vapor 3 Feb 12, 2018
@tanner0101 tanner0101 removed this from Backlog in Vapor 3 Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants