node.js, bluebird, poor control of execution path

2019-03-03 02:34发布

问题:

I'm trying to learn bluebird; I'm not controlling execution as I want to. (This bluebird question came from an async.js question at Node.js, async module, concurrency.)

Here's some code, plus what I expected to get and what I got instead.

Questions:

  • Why does the #3 function start before both (1) the end of the #1 and #2 functions, and (2) the first error check function?
  • What caused the error?
  • What happened to "end of code"?
var Promise = require('bluebird')
function part1() {
  console.log('part1 start')
  return new Promise(function(resolve, reject) {
    Promise.all(
      [ part1a(1),
        part1a(2)
      ])
    .then(
      function(err) {
        if (err) console.log('part1 error after #1 and #2')
        else console.log('part1 done with #1 and #2')
      }
    )
    .then(part1a(3))
    .then(
      function(err) {
        if (err) console.log('part1 error after #3')
        else console.log('part1 done')
      }
    )
  })
}
function part1a(i) {
  console.log('part1a start #' + i)
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      console.log('part1a done  #' + i)
      return resolve()
    }, 100)
  })
}

part1()
.then(
  function(err) {
    if (err) console.log('outermost code reported error' + err.message)
    else console.log('end of code')
  }
)

I expected

part1 start
part1a start #1
part1a start #2
part1a done  #1  // these two could
part1a done  #2  // reverse
part1 done with #1 and #2
part1a start #3
part1a done  #3
part1 done
end of code

I got

part1 start
part1a start #1
part1a start #2
part1a start #3
part1a done  #1
part1a done  #2
part1a done  #3
part1 error after #1 and #2
part1 done

Questions (repeated for reader convenience):

  • Why does the #3 function start before both (1) the end of the #1 and #2 functions, and (2) the first error check function?
  • What caused the error?
  • What happened to "end of code"?

Thanks in advance.

回答1:

It's because your call to part1a(3) is not wrapped in a function so it's get called right away instead of waiting the previous promises to be resolved:

function part1() {
    console.log('part1 start')
    // then() returns a promise so no need to create a new Promise
    return Promise.all([part1a(1), part1a(2)])
        .then(function (err) {
            if (err) console.log('part1 error after #1 and #2')
            else console.log('part1 done with #1 and #2')
        })
        // the issue was here, part1a() is a promise
        .then(function () {
            return part1a(3)
        })
        .then(function (err) {
            if (err) console.log('part1 error after #3')
            else console.log('part1 done')
        })
}

function part1a(i) {
    console.log('part1a start #' + i)
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log('part1a done  #' + i)
            return resolve()
        }, 100)
    })
}

part1().then(function (err) {
    if (err) console.log('outermost code reported error' + err.message)
    else console.log('end of code')
})