1

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.

3
  • 1
    "Doesn't support arguments"? (argument) => { ... } Commented Aug 15, 2017 at 8:21
  • 2
    arrow functions do support arguments: (arg1, arg2) => this.doSomethingWith(arg1, arg2) Commented Aug 15, 2017 at 8:23
  • If you really need to use function() { ... } instead, you can define let that = this; before, and use that in the functions instead. Commented Aug 15, 2017 at 8:23

2 Answers 2

0

I suggest you use async/await to combine and run your actions. It's easy to read, here is the basic idea, modify it with your specific case:

async function activeRegistrationASync() {
  const actionsToPerformByOrder = [
    async (r) => {
      return Promise.resolve(3)
    },
    async (r) => {
      return Promise.resolve(r + 5)
    }
  ];

  let result;
  for (let action of actionsToPerformByOrder) {
    result = await action(result);
  }
  return result;
}

activeRegistrationASync().then((r) => {
  console.log(r);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Can I chain the result of one function as the parameter of the next one without the need to know what each function should receive? I know it can be done using apply(null, arguments) but arguments doesn't work with arrow functions. I want to write a class that will build a queue of executable functions which each function result will be supplied as parameter to the next function.
but that is exactly what I'm doing, I'm passing the result of one function to the next. without the need to know what each function should receive - what do you mean here?
Yes . Thank you very much :-) I used what you wrote to build a actionsQueue class with addAction and execute functions
@user3554268, great, consider accepting my answer then)
0
  1. You can use spread operator like

    (...rest) => console.log(rest)

  2. You can use bind function

    function func (/arguments/) {console.log(this. arguments)}

    let a = func.bind(this)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.