/**
* Process and persist an entity
* @param request
* @param response
*/
@Route('PUT', '/:id')
public putOne(request: Request, response: Response): Promise<Response> {
return request.getPayload()
.then((data:any) => this.modelStore.hydrate(data))
.then((model:M) => this.modelStore.validate(model))
.then((model:M) => this.modelStore.saveOne(model))
.then((model:M) => response.data(model));
}
to this:
/**
* Process and persist an entity
* @param request
* @param response
*/
@Route('PUT', '/:id')
public *putOne(request: Request, response: Response): IterableIterator<any> {
const payload = yield request.getPayload();
const model = yield this.modelStore.validate(payload);
yield this.modelStore.validate(payload);
const savedModel = yield this.modelStore.saveOne(model);
return yield response.data(savedModel);
}
dummy POC impl:
class FooController extends ResourceController<any> {
}
let c = new FooController(null, null, null);
const iterator = c.putNOne(null, null);
var value:any;
do {
var {value, done} = iterator.next(value);
} while(!done);
const response:Response = value;
//not sure yet how to actually resolve the async data out - check if thenable, or thunk?
Pro/Con:
:+1: Simpler async expressions
:-1: Less familiar syntax for those used to promises
:-1: Needs refactor
:+1: Exceptions are easier to handle, but having both supported may mean two exception handling strategies
:+1: Iterators/Generators are all the rage
:-1: Typescript can't declare the final yielded type so every method will have signature *method():IterableIterator<any> unless I'm missing something?
Additional Comment from zakhenry
Holding off on this, as despite being super cute, not being able to strictly define the final yield type is a bit of a deal breaker
It changes this
to this:
dummy POC impl:
Pro/Con:
yield
ed type so every method will have signature*method():IterableIterator<any>
unless I'm missing something?Additional Comment from zakhenry Holding off on this, as despite being super cute, not being able to strictly define the final yield type is a bit of a deal breaker