I have a angular 4 app, and i'm trying to write a queue of actions. each action will be fired after the previous function finished and will get its 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 is that arrow functions doesn't support arguments and using function() {} instead change the this reference.
(argument) => { ... }(arg1, arg2) => this.doSomethingWith(arg1, arg2)function() { ... }instead, you can definelet that = this;before, and usethatin thefunctionsinstead.