bluebird - Chaining an array of consistent promises -
i have array of promises filtered endpoint middleware. each function in middleware returns promise single response value. makes easy chain. right i'm doing this:
bindendpoint(name, endpoint, library, fns) { // return route binding request handler return (req, res, next) => { fns.reduce( (stack, fn) => { return stack.then(fn); }, // bind asserted pass scope downwards using bluebird promise.resolve( object.assign( {}, { host: req.get('host'), req: req, res: res, next: next, data: req.body }, req.headers, req.params, req.query ) ).bind(library) ) .then((result) => { res.send(result); timer.stop(); }) .catch((err) => { res.status(err.status || 400).send({ code: err.code || 'internal_error', errors: err.errors }); }); }; }
however, on every request endpoint has use reduce fn array build stack of promises. i'd pre-build promise chain , on request of endpoint provide initial request object.
each method in chain accepts 1 request parameter it's easy fill chain downwards. i'm struggling produce pattern in bluebird (let alone native promises) accomplish this. if can reduce endpoint doing on every request, might save bit of processing time.
at minimum i'd find way optimize this, because seems bit unnecessary reduce them have.
Comments
Post a Comment