0

It feels like a bad practice to rely on an outer scoped object for holding arguments I pass to my promise. Is there a better way to do this that does not rely on the "builder"'s scope.

The code provided accomplished my objective, but it relies on the outer scope of the "builder" object.

funct1-4 are all promises

let builder = {}
funct1()
    .then(a =>{
        builder.a = a; 
        // funct2 needs a
        return funct2(builder)
    })
    .then(b => {
        builder.b = b;
        // funct3 needs a,b
        return funct3(builder)
    })
    .then(c =>{
        builder.c = c;
        // funct4 needs a,b,c
        funct4(builder)
    })
Test1 Test2
  • 327
  • 2
  • 9
  • 3
    You can use `Async/Await` – Code Maniac Jun 07 '19 at 13:04
  • 1
    Well, you can just instantiate that object in the first callback and it wouldn't be from outside scope any more. – VLAZ Jun 07 '19 at 13:05
  • 2
    Maybe you could have your `functX` return the "enhanced builder" rather than just the additional property to append? I.e. `funct1().then(builder => funct2(builder)).then(builder => funct3(builder))...`. – sp00m Jun 07 '19 at 13:09
  • VLAZ I don't think this works. Am I understanding correctly: declare "let builder = {}" in the first "then" block after funct1... But then I will not be able to use it in subsequent "then" blocks – Test1 Test2 Jun 07 '19 at 13:10
  • sp00n I am thinking about when functX is a 3rd party function. So I think I could wrap the 3rd party function and then return the "enhanced builder" – Test1 Test2 Jun 07 '19 at 13:14

0 Answers0