异步eachseries节点js

时间:2018-11-05 18:22:41

标签: javascript node.js async.js

为什么嵌套在每个系列中的每个系列不一致?

我想做一个简单的例子:

const arr = [1, 2, 3, 4, 5]
async.eachSeries(arr, function(item, callback) {
  console.log('check: ' + item)

  const arr2 = ['a', 'b', 'c']
  async.eachSeries(arr2, function(item2, callback2) {
    console.log('item: ' + item2)

    if (item2 !== 'b') {
      console.log('new')
      callback2()
    } else {

      console.log('done')
      callback2()
    }

  })

  callback()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/async/2.6.1/async.min.js"></script>

哪个正确地给了我

check 1
item a
item b
item c
check 2
item a
item b
item c
check 3...

但是当我这样做时:

connection.query("SELECT * FROM `offers` WHERE `status` = ? AND date = ?", ['0', '16:15'], //there im getting news which posted (telling about wear shop)
  function(err, res) {
    if (res.length > 0) { //IF /\ NOT EMPTY

      var tags = res[0].wear.split('/'); //there im splitting wear sizes like  S / M / X / XL / XXl 

      async(tags, function(t, callback) { // 1 operations - check 1 size
        console.log('start works at tag: ' + t) //which size of wear bot checking

        connection.query("SELECT * FROM `users_tag` WHERE `tag_id` LIKE '%" + t + "%'", //searching size which user want |  there i got all users who have size which searching (L)-as example
          function(e, r) {


            async(r, function(ro, callback2) { //now working with every user who have size (L)-as example

              console.log('user: ' + ro.user_id) //telling about user who under process

              connection.query("SELECT * FROM `offers` WHERE `id` = ? AND `did` NOT LIKE '%" + ro.user_id + "%'", [res[0].id], //looking for user did not get message before
                function(error, result) {
                  if (result.length > 0) { //if user did not get message im saving him as user who already got message
                    if (res[0].did !== '-') { // if field with users who already got message is NOT empty
                      var did = res[0].did + ',' + ro.user_id
                      connection.query("UPDATE `offers` SET `did` = ? WHERE id = ?", [did, res[0].id],
                        function(er, re) {

                        });
                    } else {
                      connection.query("UPDATE `offers` SET `did` = ? WHERE id = ?", [ro.user_id, res[0].id], // if field with users who already got message is EMPTY
                        function(er, re) {

                        });
                    }

                    telegram.telegram.sendMessage(ro.user_id, '[#](' + res[0].img + ')' + res[0].offer, arkup) //there sending message to user
                    console.log('send to: ' + ro.user_id)
                    callback2() //there start working with next user
                  } else { //if user already GOT message before
                    console.log(ro.user_id + ' got message')
                    callback2() //there start working with next user

                  }
                });
            })

          });
        callback(); //there start working with next size

      })
    }
  });

我没有相同的订购。我明白了:

start works at tag:  M
start works at tag:  S
user: 464398822
user: 464398822
464398822 got message
user: 382065027
464398822 got message
user: 382065027
382065027 got message
user: 595293617
382065027 got message
user: 595293617
595293617 got message
595293617 got message

我想查询一个标签,检查所有选择此大小的用户并向他们发送消息,然后才对下一个标签执行相同操作。而是,订购不同步。

0 个答案:

没有答案