typescript - Angular - How to use arrow functions with arguments? -
i have angular 4 app, , i'm trying write queue of actions. each action fired after previous function finished , parameters.
public activeregistrationandsync() { let actionstoperformbyorder = [ () => this.registrationcomponent.activeformvalidators.apply(null, arguments), () => this.registrationcomponent.register.apply(null, arguments), () => this.autologin.apply(null, arguments), () => this.postlogin.apply(null, arguments), () => this.checkout.apply(null, arguments) ]; let defer = new deferred<void>(); let curpromise = defer.promise; //build chain of acitons actionstoperformbyorder.foreach(action => { curpromise = curpromise .then(action) .catch(err => this.error.emit(err)); }) //active actions defer.resolve(); } export class deferred<t> { promise: promise<t>; resolve: (value?: t | promiselike<t>) => void; reject: (reason?: any) => void; constructor() { this.promise = new promise<t>((resolve, reject) => { this.resolve = resolve; this.reject = reject; }); } }
my problem arrow functions doesn't support arguments , using function() {} instead change reference.
i suggest use async/await
combine , run actions. it's easy read, here basic idea, modify specific case:
async function activeregistrationasync() { const actionstoperformbyorder = [ async (r) => { return promise.resolve(3) }, async (r) => { return promise.resolve(r + 5) } ]; let result; (let action of actionstoperformbyorder) { result = await action(result); } return result; } activeregistrationasync().then((r) => { console.log(r); });
Comments
Post a Comment