Search code examples
javascriptpromisechaining

JS Promise: control flow


I have to set a few things up via an API. It's important that the different functions are executed after each other. All of the listed functions return a proper promise.

a(analyticsConfig._listConfig)
    .then(function() {
        return b(listName, analyticsConfig._fieldConfig);
    })
    .then(function() {
        return c(listName)
    })
    .then(function() {
        return d(analyticsConfig._roleConfig);
    })

I'd like to use something like a.then(b(listName, analyticsConfig._fieldConfig)) or so but as you probably know this won't work.

Is there another way to do this?


Solution

  • You can write it as

    a.then(b.bind(null, listName, analyticsConfig._fieldConfig))
    

    or, if you're either using babel to transpile your code, or you're targeting a version of node > v4., you can do

    a.then(() => b(listName, analyticsConfig._fieldConfig))