How to sync JavaScript callbacks?

前端 未结 6 1403
长发绾君心
长发绾君心 2020-12-01 08:54

I\'ve been developing in JavaScript for quite some time but net yet a cowboy developer, as one of the many things that always haunts me is synching JavaScript\'s callbacks.<

6条回答
  •  孤城傲影
    2020-12-01 09:19

    The second problem is not really a problem as long as every one of those is in a separate function and the variable is declared correctly (with var); local variables in functions do not interfere with each other.

    The first problem is a bit more of a problem. Other people have gotten annoyed, too, and ended up making libraries to wrap that sort of pattern for you. I like async. With it, your code might look like this:

    async.each(someArray, myFunc1, myFunc2);
    

    It offers a lot of other asynchronous building blocks, too. I'd recommend taking a look at it if you're doing lots of asynchronous stuff.

提交回复
热议问题