N
N
nuclear_kote2016-09-24 19:26:18
Angular
nuclear_kote, 2016-09-24 19:26:18

How to rewrite defer in Angular 2?

There are 2 functions like this:

function func1(bla) {
          var deferred;
           if (condition) {
                 bla.forEach(function (val) {
                     deferred = $q.all(func2(val));
                 })
            } else {
                deferred = func2(bla);
            }
            return deferred;
    }

function func2(bla) {
    var defer = $q.defer();
    if (cond2) {
        defer.resolve(...);
    } else {
       defer.reject(...);
    }
    return defer.promise;
}

How can this be rewritten in Angular 2?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Lopatin, 2016-09-25
@nuclear_kote

By using Promise directly. You should get something like this:

function func1(bla: any | any[]): Promise<any> {
  if (bla instanceof Array) {
     return Promise.all(bla.map(func2));
  }
  return func2(bla);
}

function func2(bla: any): Promise<any> {
  return new Promise((resolve, reject) => {
    if (cond2) {
      resolve(...);
    } else {
      reject(...);
    }
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question