I am currently working on an Angular 4 app and I am attempting to create a queue of actions. Each action should only be executed after the previous one has finished, and each action should receive its own set of 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 actions
actionsToPerformByOrder.forEach(action => {
curPromise = curPromise
.then(action)
.catch(err => this.error.emit(err));
})
//Execute 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;
});
}
}
The issue I am facing is that arrow functions do not support arguments, and using function() {} instead changes the reference of 'this'.