1

I am using a loop in my function which is calling another function I wanted it to wait till I get the result then go to next iteration.I am using promise but it is not working as well. Marked ** in that portion.

  function imagecheck(url) {
    return new Promise(function(resolve, reject){
        var img = new Image();
        img.onload = function(){
            resolve('success');
        }
        img.onerror = function(){
            reject('failure');
        }
        img.src = url;
    });
}

function getcheck(Id) {
    var j=0;
    var myId = Id;
    var myimages = [];
    **for(var i=0;i<=j;i++)
   {
        var imageUrl = 'someprexixurl'+myId+'&page='+j;   
        this.imagecheck(imageUrl).then(function(successUrl){
            myimages.push({url:imageUrl});
            j++;
        }).catch(function(errorUrl){

            });
   }**
}
2

You'll want to use async/await to easily allow your loop to wait. Here's what that looks like:

async function getcheck(Id) {
    var j=0;
    var myId = Id;
    var myimages = [];
    for(var i=0;i<=j;i++)
    {
        try {
            var imageUrl = 'someprexixurl'+myId+'&page='+j;
            var data = await this.imagecheck(imageUrl);
            myimages.push({url:data});
            j++;
        } catch(e) {
            // Error handling here //
        }
   }
}
| improve this answer | |
  • Thank you! however I should have put this question in salesforce lightning context looks like async is not supported in lightning. – Ruchit612 Aug 29 '19 at 12:37
  • @Ruchit612 There should be a way. I'll see if I can write a demo. – sfdcfox Aug 29 '19 at 12:43
  • Sure, However I have created a new question and put it in salesforce lightning context, If you get any answer you can put it there. Thank you. – Ruchit612 Aug 29 '19 at 12:47
0

I am able to do it in salesforce, I just kept the loop for only 25 times as per my requirement and I am setting total length of my completed array to 'counter' attribute. So, Basically when my getcheck function will be called it will give the result once imgArray have all the valid url's. Hope it will help others:

 imagecheck: function(url) {
        return new Promise(function(resolve, reject){
            var img = new Image();
            img.onload = function() {
                resolve(url);
            }
            img.onerror = function(){
                reject('failure');
            }
            img.src = url;
        });
    },
    getcheck: function(component, callback) {
        var myId = component.get("v.currentversionobj").Id;
        var imgArr = [];
        var flag=0;
        for(var i=0; i<=25; i++) {
            var imageUrl = 'someprefixurl'+component.get("v.currentversionobj").Id+'&page='+i;   
            this.imagecheck(imageUrl).then(function(successUrl) {
                imgArr.push({url:successUrl}); 
                component.set("v.fileShares",imgArr);
            }).catch(function(errorUrl){
                flag=1;
            });        
        }
        var myVar = setInterval(function(){
            if(flag == 1){
                component.set("v.counter",imgArr.length);
                clearInterval(myVar);
                callback(true);
            }
        }, 2000);
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.