The process of traversing arrays and returning values in node.js is really confusing. …

  node.js, question
var a = [[1,2,3],[4,5,6],[7,8,9]]
 var b = []
 
 const co = require('co');
 
 new Promise(function(resolve, reject) {
 a.forEach(function(item) {
 item.forEach(function(it) {
 setTimeout(function () {
 b.push(it)
 }, 1000);
 })
 })
 // resolve(b)
 }).then(function(data){
 console.log(data);
 })
 
 //Is it mainly an asynchronous problem or something, it will directly return b = [] instead of expecting it
 // b = [1,2,3,4,5,6,7,8,9]

Promise changed to resolve state before the timer was executed, and then’s callback output B was executed.
Then the timer will time out and put the data into B inside.
Resolve after the timer has completely expired, not directly after setting the timer.

new Promise(function(resolve, reject) {
 // code
 // resolve(b)
 }).then(function(data){
 console.log(data);
 })

This promise has neither resolve nor reject, so it has always been pending, not console.log So I guess you should have forgotten to take out the comments in resolve.

a.forEach(function(item) {
 item.forEach(function(it) {
 setTimeout(function () {
 b.push(it)
 }, 1000);
 })
 })
 resolve(b)

Your promise contains these codes, the traversal of two-dimensional arrays. For each element of this two-dimensional array, set a timer, which adds the element to B inside after 1 second. After setting the timer, promise resolve first. At this time, it has not been 1 second, the timer has not been executed, and B is still an empty array. However, since you have resolve, you have executed your sentence after then, and this is the time you have encountered the problem.
If you wait a while (1 second later) and then output A, it is actually the result you want. After one second, these timers set will time out and trigger, then push the element to B inside.

The result you want should be written like this.

var a = [[1,2,3],[4,5,6],[7,8,9]]
var b = []

new Promise(function(resolve, reject) {
  a.forEach(function(item) {
    item.forEach(function(it) {
      setTimeout(function (item, it) {
        b.push(it)
          if(item == a[a.length-1] && it == item[item.length -1]) {
          resolve(b);
        }
      }, 1000, item, it);
    })
  })
}).then(function(data){
  console.log(data);
})

At each timer inside judges that if I am the last of the two-dimensional array inside, then I will resolve. This will do.