繁体   English   中英

Q.all不从数组中调用promise

[英]Q.all not calling promises from array

伙计们 我正在尝试迁移用户,但从未兑现承诺。

  User = mongoose.model 'User'
  User.find({"hisId" : {$exists : true}}).exec (err, doc)->
    if err
      console.error err
      process.exit()
    if not doc or doc.length < 1
      process.exit()
    i = 0
    promiseArray = []
    while i < doc.length
      updateFunc = (doc) ->
        defer = Q.defer()
        User.update({
          "_id" : ObjectId(doc[i].juniorId)
          "friends.user": ObjectId(doc[i]._id)
        },{
          $set:
            "friends.$.canAdmin" : true
        }, (err, updateResult) =>
          if err
            console.error err
            defer.reject()
          defer.resolve()
        )
        return defer.promise
      updateFunc.bind(null, doc)
      promiseArray.push updateFunc
      i++
    Q.all(promiseArray).then(->
      console.log 'Why did get here before promises all fulfilled?'
    ).done ( ->
      process.exit()
    )

我尝试使用Q.promise解决方案,但是它也不起作用。 Promise数组充满了功能,我检查了一下。

promise数组中填充有函数,但未填充promise,因此已实现。

而是做promiseArray.push(updateFunc(doc))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM