B
B
bio2016-08-16 16:24:49
Angular
bio, 2016-08-16 16:24:49

How to correctly inject Promise dependency into $injector.invoke?

Good afternoon!
There is an object:

resolve = {
    promise1: (MyService) => MyService.all(),
    promise2: (MyService2, promise1) => { return promise1.then((data) => MyService2.getData(data.id))},
}

It is processed like this:
angular.forEach(resolve, function (value, key) {
    resolve[key] = $injector.invoke(value, null, resolve, key);
});

$q.all({
    ...
    locals: $q.all(resolve)
}).then(function (data) {
    ...
}

The problem is that in promise2, the promise1 dependency comes as a Promise, although it is more logical to expect the result there, i.e. would look like this:
promise2: (MyService2, promise1) => MyService2.getData(promise1.id)

I found a way, but I don’t like it, it really looks like a crutch:
$q.all({
    ...
    locals: $q.all(resolve)
}).then(function (data) {
    
    resolve.promise2.then((item) => {
        resolve.promise2 = item;
    });
    ...
}

Is there another way to get the result of a Promise as an argument?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-08-16
@AMar4enko

Why didn't you like the first option? What should I write .then()?
And in order not to write extra 20 characters, do you want to make a crutch?
Then I don't understand your train of thought.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question