3

I'm attempting to run a series of functions based upon the Q API using their first strategy for sequences. This suggests the pattern:

var funcs = [foo, bar, baz, qux];

var result = Q(initialVal);
funcs.forEach(function (f) {
    result = result.then(f);
});
return result;

What structure are each of the functions within the array meant to take? I am quite confused about when to use the return def.promise;. Is that simply always the last line? Will it frequently or always immediately follow def.resolve(someVar). Is something like this then ended structure?

function foo(f){
    var def =  Q.defer();
    f++;
    def.resolve(f);
    return def.promise;
}

So that each subsequent function within the array will receive the newly calculated value of f: in this case, if var initialVal = 1; and four functions each incrementing f++, the returned result will be 4? How do I access that returned value? console.log(result) prints { state: 'pending' } .

1252748
  • 14,597
  • 32
  • 109
  • 229
  • If you want, you can try some code I wrote a few days ago. You can find it on http://codereview.stackexchange.com/questions/98134/function-queue-for-synchronous-execution. Sadly, I haven't published the reviewed version on github. But, if you are interested, I can do it today. Basically, you add functions to a queue that executes them sequentially. It requires a delay in milliseconds, but the number of functions you can run is unlimited. Or I've misunderstood the question? – Ismael Miguel Aug 05 '15 at 16:04
  • 1
    @IsmaelMiguel Thank you very much. No, you've not misunderstood the question. However, this is a bit complex for my needs as I'm just learning about this in the most basic of ways. I'm more looking for how to this with a known number of functions in the queue, so I can eventually grow to greater levels of complexity. Thank you very much though. I've bookmarked your function :) – 1252748 Aug 05 '15 at 16:09
  • I'm sorry, but I don't understand this bit: "I'm more looking for how to this with a known number of functions in the queue". Perhaps there is a missing word. – Ismael Miguel Aug 05 '15 at 16:11
  • 1
    @IsmaelMiguel How do do it if the number of functions in the queue is known. – Mast Aug 05 '15 at 16:12
  • 1
    @IsmaelMiguel "...how to *do* this..." ;) – 1252748 Aug 05 '15 at 16:13
  • @thomas Using the code I've provided, you can use the `.add` method to add multiple functions. At the moment, you can only add 1 at a time, but you can add multiple functions. This means that you will need to run `FnQueue.add` for each function. I hope I was clear enough. If I wasn't, please excuse me. If you want, I may write an answer with this content. – Ismael Miguel Aug 05 '15 at 16:18
  • @IsmaelMiguel No, this isn't what I want. All I want is a 5-7 line example of how to structure the functions `foo`, `bar` etc so that they may be run in sequence, values returned by one, can be accessed by the next, and how I can access the final value. In short, just a fleshing out of the example provided by the section in the API I linked. Thank you though. – 1252748 Aug 05 '15 at 16:23
  • Excuse me, but I'm a little confuse. Can we move this discussion to a chatroom, so I can understand what I really need? – Ismael Miguel Aug 05 '15 at 16:26
  • @IsmaelMiguel I really don't know how I could be any more clear I'm afraid. – 1252748 Aug 05 '15 at 16:30
  • @thomas I'm trying to find a way to create a room for you to join in, so I can ask every question without disturbing or flooding. So far, I've failed – Ismael Miguel Aug 05 '15 at 16:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85222/discussion-between-ismael-miguel-and-thomas). – Ismael Miguel Aug 05 '15 at 16:39

1 Answers1

2

What structure are each of the functions within the array meant to take?

Q.js allows promises to be created in several ways. For example :

function foo(value) {
    var def =  Q.defer();
    def.resolve(value + 1);
    return def.promise;
}

function foo(value) {
    return Q(value + 1);
}

function foo(value) {
    return Q.Promise(function(resolve, reject) {
        resolve(value + 1);
    });
}

Other Promise libs are similar, but not necessarily so flexible. Native js Promises must be constructed with the third of these approaches.

However, in the real world you will only rarely need to create your own Promise. You will typically be dealing with promise-returning lib methods that someone else has written. For example :

function foo(value) {
    return lib.doSomethingAsync(value, and, other, params);
}

How do I access that returned value?

The code is easier to understand if member name "result" is replaced with "promise", and result.then(f) is rewritten with an anonymous function that calls f().

function performAsyncSequence() {
    var promise = Q(initialVal);
    funcs.forEach(function (f) {
        promise = promise.then(function(previousResult) {
            return f(previousResult);
        });
    });
    return promise;
}

This is 100% equivalent to the code in the question, but now it should be clearer how the previous result is passed down the promise chain.

Accessing all previous promise results in the sequence is more complicated. The answers here discuss the subject comprehensively.

Community
  • 1
  • 1
Roamer-1888
  • 19,138
  • 5
  • 33
  • 44