Click here to Skip to main content
15,867,453 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: Can't figure out why I'm not able to access my projects array within my JSON object Pin
jkirkerx25-Nov-21 7:37
professionaljkirkerx25-Nov-21 7:37 
Questionwait for fetch to finish within a for each loop Pin
jkirkerx16-Nov-21 13:35
professionaljkirkerx16-Nov-21 13:35 
I thought this would be easy, but didn't consider my for each loop just running and calling fetch really fast, when I need to call fetch one at a time until it finishes. Or maybe not if I consider the web server being able to process that many fetches, say 30 to 60, but I would hate to load up the web server with that many jobs.

I have this long process, that may take 2 or 3 minutes each, and didn't want to send a large payload of jobs to the API, so I broke the payload down into individual jobs to send to the API one at a time. I extracted the payload and ran each payload in a loop. I'm not sure if I need to tell the loop to wait, or tell fetch to wait. I've never done this before in JavaScript and I'm not sure how to approach this. Tried a few things, but I probably need to write test code that reflects the principals first and confirm that it works, and then adjust my code. Everything I found on the Internet didn't really match what I wanted to do.

I pasted my function so you can see what I'm trying to achieve. I've worked with promise before in Angular, waiting for google API to validate my google credentials but that was a single transaction.
async function runProjectUpdateCostAsync() {

    const vendorId = document.getElementById('updateProjectCost_vendorId').value;
    const bodyPayload = JSON.parse(document.getElementById('updateProjectCost_jsonPayload').value);
    const priceLists = JSON.parse(bodyPayload.priceLists);

    // Program the Api Url
    let apiUri = document.getElementById('updateProjectCost_apiUri').value;
    apiUri += '?updateProjectCost=true';

    // Were now looping through priceList to shorten the time
    let promise = Promise.resolve();
    for await (const priceList of priceLists) {

        const statusElement = document.getElementById(priceList.statusElement);
        const img = new Image();
        img.src = '/assets/images/Gear-0.2s-32px.gif';
        img.alt = 'wait';
        statusElement.innerHTML = '';
        statusElement.appendChild(img);

        const newBodyPayload = JSON.stringify({
            "vendorId": vendorId,
            "priceLists": "[" + JSON.stringify(priceList) + "]"
        });

        const span = document.createElement('span');
        span.classList.add('font-weight-bold');
        span.classList.add('mx-2');

        promise = promise.then(fetch(apiUri, {
            method: 'POST',
            body: newBodyPayload,
            headers: {'Content-type': 'application/text; charset=utf-8'}
        })
            .then(response => response.json())
            .then(data => {

                span.classList.add('text-success');
                span.innerHTML = 'Success';
                statusElement.innerHTML = '';
                statusElement.appendChild(span);

            })
            .catch(e => {

                span.classList.add('text-danger');
                span.innerHTML = 'Failed';

                statusElement.innerHTML = '';
                statusElement.appendChild(span);

                console.error("runProjectUpdateCost Error: ", e);

            }));

    }

}
If it ain't broke don't fix it
Discover my world at jkirkerx.com

AnswerRe: wait for fetch to finish within a for each loop Pin
Richard Deeming16-Nov-21 22:49
mveRichard Deeming16-Nov-21 22:49 
GeneralRe: wait for fetch to finish within a for each loop Pin
jkirkerx17-Nov-21 5:52
professionaljkirkerx17-Nov-21 5:52 
QuestionRecommended Apps to learn java for student? Pin
kennymanda11-Nov-21 19:44
professionalkennymanda11-Nov-21 19:44 
AnswerRe: Recommended Apps to learn java for student? Pin
OriginalGriff11-Nov-21 19:45
mveOriginalGriff11-Nov-21 19:45 
GeneralMessage Closed Pin
16-Dec-21 11:25
cryptorobotics16-Dec-21 11:25 
AnswerRe: Recommended Apps to learn java for student? Pin
Richard MacCutchan11-Nov-21 21:04
mveRichard MacCutchan11-Nov-21 21:04 
AnswerRe: Recommended Apps to learn java for student? Pin
jhonaa28-Mar-22 0:10
jhonaa28-Mar-22 0:10 
QuestionUploading image using JQuery AJAX Pin
Alex Dunlop10-Nov-21 18:56
Alex Dunlop10-Nov-21 18:56 
AnswerRe: Uploading image using JQuery AJAX Pin
Richard Deeming10-Nov-21 21:44
mveRichard Deeming10-Nov-21 21:44 
GeneralRe: Uploading image using JQuery AJAX Pin
Alex Dunlop10-Nov-21 22:14
Alex Dunlop10-Nov-21 22:14 
GeneralRe: Uploading image using JQuery AJAX Pin
Richard Deeming10-Nov-21 22:17
mveRichard Deeming10-Nov-21 22:17 
AnswerRe: Uploading image using JQuery AJAX Pin
jkirkerx16-Nov-21 13:50
professionaljkirkerx16-Nov-21 13:50 
GeneralRe: Uploading image using JQuery AJAX Pin
Richard Deeming17-Nov-21 0:46
mveRichard Deeming17-Nov-21 0:46 
GeneralRe: Uploading image using JQuery AJAX Pin
jkirkerx17-Nov-21 6:18
professionaljkirkerx17-Nov-21 6:18 
Questionhow do you paint objects with two different colors in the for repetition structure Pin
Member 1541326530-Oct-21 3:38
Member 1541326530-Oct-21 3:38 
AnswerRe: how do you paint objects with two different colors in the for repetition structure Pin
Richard MacCutchan30-Oct-21 4:25
mveRichard MacCutchan30-Oct-21 4:25 
GeneralRe: how do you paint objects with two different colors in the for repetition structure Pin
Member 1541326530-Oct-21 9:07
Member 1541326530-Oct-21 9:07 
AnswerRe: sytax error unexcepted token Pin
OriginalGriff5-Oct-21 21:20
mveOriginalGriff5-Oct-21 21:20 
GeneralRe: sytax error unexcepted token Pin
Richard Deeming5-Oct-21 21:44
mveRichard Deeming5-Oct-21 21:44 
GeneralRe: sytax error unexcepted token Pin
OriginalGriff5-Oct-21 21:52
mveOriginalGriff5-Oct-21 21:52 
Questiondata import via API Pin
Francesco Reboldi23-Sep-21 19:28
Francesco Reboldi23-Sep-21 19:28 
AnswerRe: data import via API Pin
Afzaal Ahmad Zeeshan23-Sep-21 23:47
professionalAfzaal Ahmad Zeeshan23-Sep-21 23:47 
GeneralRe: data import via API Pin
Afzaal Ahmad Zeeshan24-Sep-21 4:11
professionalAfzaal Ahmad Zeeshan24-Sep-21 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.