2

Hi want to set the timeout funtion for the Freach loop for the callback i am setting the timeout funtion. but its displaying Undifined error. can anybody Explain me here is My code.

function async(array,cb){

    array.forEach(function () {
        setTimeout(cb,0);

    })

}

async([1,2,3,4],function(i){
    console.log(i);
});

1 Answer 1

3

Try:

function async(array,cb){
    array.forEach(function(e) {
        setTimeout(function() { cb(e); },0);

    });
}

async([1,2,3,4,5], function(i) {
  console.log(i);
});

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, thanks its Working, can you please explain me what the error in my code.
@Jeevan You need to pass the argument from forEach to your function, to do so you need to wrap that function with anonymous function and call your function with argument from forEach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.