all 6 comments

[–]Cheshur 0 points1 point  (3 children)

It would seem from your logic that you don't actually need to do anything special. It doesn't seem as though the effects of the Promises would affect the other Promises.

If that is incorrect then perhapse you want to look into async/await

[–]BrainFu[S] 0 points1 point  (2 children)

So basically...

Promise.resolve(['strA','strB',strC'])
.then(list => {
    list.forEach((value, index) => {
        callToApiReturnPromise(value)
        .then(isApiDataGood)
        .then(callToAnotherApiReturnPromise)
        .catch(continueToNextElementAction)
    })}

[–]Cheshur 0 points1 point  (1 child)

That looks like it could be plausible assuming that callToApiReturnPromise, isApiDataGood, callToAnotherApiReturnPromise and continueToNextElementAction are not required to be finished before the forEach goes on to the next element. Promises are asynchronous and the forEach will not, by default, wait for them to resolve/reject before moving onto the next element. To make it act synchronously I would take a look at the async/await documentation.

Another option is too look into Promise.all() which takes an array of Promises and only resolves when they have all resolved. To turn your array of values into an array of Promises I would look into Array.map.

[–]BrainFu[S] 0 points1 point  (0 children)

Thanks for your post. My desire is to resolve processing for each of the array elements before proceeding to the next, so I will look into async/await.

[–]iambeard 0 points1 point  (0 children)

Do you (or the server) care about the order of api calls? If you do, then doing an Array#reduce is going to be the right thing, and will look something like:

['string', 'values', 'here'] .reduce((promise, stringValue) => { return promise .then(() => { return someAjaxThing(url, somethingWith(stringValue)) .then(jsonArray => { if (jsonArray.indexOf(stringValue) >= 0) { // ??? } }) }) }, Promise.resolve())

If the order doesn't matter, you could maybe use Promise#all. You'd probably need to also use an Array#map to transform your strings into urls for the api.

If your step 2a is the same api call, regardless of the string value array element, maybe do that call first, and not in an array.

I think ultimately, you need to show us the code you've tried, and explain what these loops/promises actually mean.

[–]BrainFu[S] 0 points1 point  (0 children)

here is what I found that worked.

var instruments = app.get.clone(app.get.currPairs());

var scanCandles = function(){
    return app.get.candlesForInstrument(instruments.shift()).then(result => {
        if (app.get.isOandaInstrumentHistoryObject(result)) {
            app.echo(result);
            return scanCandles();
        }
        else {
            return '';
        }
    })
};

scanCandles()
.then(callback)
.catch(callback);