0

I'm trying to store a series of functions in an array with their parameters, and then execute them sequentially. I've been using this question: How to chain execution of array of functions when every function returns deferred.promise? And a specific answer to this question: http://plnkr.co/edit/UP0rhD?p=preview.

From my understanding this can be done with an object literal or an array. I've read some questions where people store functions without parameters in arrays, but so far haven't found a good answer on including them with parameters.

Below is my attempt. I'm creating an array of functions with their parameters first (hard-coded for now), and then I pass them to ExecutePromiseChain() for execution. From what I see it looks like the functions are called immediately, which I don't want.

Responses = [];
function BuildInventoryList(){
    return new Promise(function(resolve, reject) {
        f = [new CreateRequestWPromise(`https://${defaults.host}/inventory/88421`, {}),
    new CreateRequestWPromise(`https://${defaults.host}/inventory/19357`,{})];

        resolve(ExecutePromiseChain(f));
});
}

function ExecutePromiseChain(funcs){
    var promise = funcs[0];
    for (var i = 1; i < funcs.length; i++){
        promise = promise.then(funcs[i]);
    }
    return promise;
}

Note that I'm returning promise in ExecutePromiseChain() so I can chain to it later.

And this is my promisified http request function:

function CreateRequestWPromise(url, body){
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
        xhr.onload = function(){
            if(xhr.status == 200){
                Responses.push(JSON.parse(xhr.response));
                resolve(xhr.responseText);
            }
            else{
                reject(xhr.statusText);
            }
        }
        xhr.onerror = function() {
            reject(Error("Network Error"));
        };

    xhr.open("POST", url);
    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    var bodyS = JSON.stringify(body);
    xhr.send(bodyS);
});
}

I call these functions at the start of the program with:

BuildInventoryList().then(CreateRequestWPromise(`https://${defaults.host}/inventory/update`, 
    Items));

So what dumb mistakes am I making here? Why aren't my functions execution sequentially as they should be?

As is probably obvious I'm still learning the ropes of Javascript and promises. I appreciate the patience and help :)

no_parachute44
  • 365
  • 1
  • 15
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! `ExecutePromiseChain` already returns a promise, `BuildInventoryList` should not use `new Promise`. – Bergi Oct 19 '17 at 22:52
  • Your `f` appears to be an array of promises, not of functions that return promises. – Bergi Oct 19 '17 at 22:52
  • `Why aren't my functions execution sequentially` - because calling `CreateRequestWPromise` executes the `XMLHttpRequest` – Jaromanda X Oct 19 '17 at 23:08

1 Answers1

0
.then(someFunction(...))

You just called someFunction() and pass its result to then() (just like any other parameter).

You need to pass a function that calls it with the parameters you want:

.then(() => someFunction(...))
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964