Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angularjs promise .then() to run sequentially inside a for loop

function somethingAsync(value) {
    return new Promise(res => setTimeout(res, 500, value * value));
}

function processInputInSeries(input) {
    function processNext(index, input, results) {
        if (index >= input.length) return results;
        return somethingAsync(input[index]).then(function (result) {
            return processNext(index + 1, input, [...results, result]);
        });
    }

    return processNext(0, input, []);
}

const resultPromise = processInputInSeries([1,2,3]);
resultPromise.then(function(results) { console.log(results) });
Comment

PREVIOUS NEXT
Code Example
Javascript :: AngularJS w/Prerender 404 error on home page 
Javascript :: how to replace img url using jquery on mobile screen 
Javascript :: directive with ng-if not rendering in Angular.js 
Javascript :: angularjs getting Error: [$rootScope:inprog] $digest already in progress when changed from Fetch to $http + $q 
Javascript :: angularjs How to sort a specific value in a map 
Javascript :: object Promise showing instead of data pulled from API call 
Javascript :: How to hide div based on select the dropdown in angular js 
Javascript :: Relaxed "angularjs" style expression parsing missing in vue 
Javascript :: react-native navigation stack set push component then cover parent page 
Javascript :: Context: Cannot read properties of undefined 
Javascript :: Undefined value document.getElementById 
Javascript :: chain underscore 
Javascript :: get the character code in a string 
Javascript :: How to access a preexisting collection with Mongoose 
Javascript :: Javascript if time is between 7pm and 7am do this? Javascript If Statement Time Action 
Javascript :: No enum constant datepicker react native 
Javascript :: Appium find Android Element with Xpath using Javascript 
Javascript :: Sending An AJAX Request Using Ky 
Javascript :: phaser asteroid movement 
Javascript :: Accessing Our CryptoCurrency blockchain through local server 
Javascript :: React Using Self Made Module 
Javascript :: js a || b 
Javascript :: air config file 
Javascript :: CUSTOM_ELEMENTS_SCHEMA 
Javascript :: nestjs prisma controller 
Javascript :: docker healthcheck express 
Javascript :: sort items 
Javascript :: how to render react quill the way it is without the html tags 
Javascript :: add even javascript 
Javascript :: jquery loop through model list 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =