CanadaRox
CanadaRox

Reputation: 300

Is there a cleaner way to chain these Bluebird promises?

I have three functions (A, B, C) that each return a promise. The chain of promises don't require any information from the previous promises except that they complete.

B has to wait for A to finish, and C has to wait for B to finish.

Currently I have:

return A(thing)
.then(function () {
  return B(anotherThing);
})
.then(function () {
  return C(somethingElse);
});

This feels like I'm wasting a lot of space (7 lines for what is really only 3 lines of actual code).

Upvotes: 0

Views: 63

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

This works

return A(thing)
    .then(B.bind(null,anotherThing))
    .then(C.bind(null,somethingElse));

note: bind is not available on IE8 or earlier - but there's a polyfill - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

For info - in ES2015 you could do - iojs will let you enable arrow functions, but they are apparently broken in some way

return A(thing)
    .then(() => B(anotherThing))
    .then(() => C(somethingElse));

Upvotes: 3

Related Questions