然后在蓝鸟中调用函数

Call functions in bluebird's then

如何在 then() 中调用函数?我是 Bluebird 的新手,之前我只是触发回调以通知被调用的函数已完成它的工作。

这是我的 "main code",它将语言环境数据插入到我的 Mongo 和 Redis 数据库中。

var promise = Language.findOne({}).exec()

promise.then(function(languages) {
    /* If collection is empty initialize it */
    if(!languages)
        return insertLanguagesIntoMongoDB()
})
.then(function() {
    /* Make sure it's cached in Redis */
    return insertLanguagesIntoRedis()
})
.catch(function(err) {
    throw err
})

这就是我的函数的样子,但是它说 "callback is not a function":

function insertLanguagesIntoRedis(callback) {
    logger.info("Inserting languages into redis db")
    var promise = Language.find({}).exec()
    promise.then(function(languages) {
        if(!languages)
            throw new Error("Couldn't find any languages in MongoDB's Language collection")

        var languageJson = []
        for(var i=0; i<languages.length; i++) {
            var object = {}
            object.id = languages[i].iso_code
            object.text = languages[i].name_en
            languageJson.push(object)
        }
        redis.set('languages', JSON.stringify(languageJson))
        return callback()
    })
    .catch(function (err) {
        throw err
    })
}

那么我该如何正确定义和调用我的 insertLanguagesIntoRedisinsertLanguagesIntoMongoDB 函数?

按照这些思路可能会起作用:

  1. 将语言传递到承诺链中
  2. 在链条的末端return进入下一步的承诺
  3. 调整下一步以接受语言
  4. 调整 insertLanguagesIntoRedis 以接受语言
  5. 有 insertLanguagesIntoRedis return 承诺链
  6. 从 insertLanguagesIntoRedis 中删除回调调用

var promise = Language.findOne({}).exec()

promise.then(function(languages) {
    /* If collection is empty initialize it */
    if(!languages)
        insertLanguagesIntoMongoDB()
     return languages
})
.then(function(languages) {
    /* Make sure it's cached in Redis */
    return insertLanguagesIntoRedis(languages)
})
.catch(function(err) {
    throw err
})

function insertLanguagesIntoRedis(languages) {
    logger.info("Inserting languages into redis db")
    var promise = Language.find({}).exec()
    return promise.then(function(languages) {
        if(!languages)
            throw new Error("Couldn't find any languages in MongoDB's Language collection")

        var languageJson = []
        for(var i=0; i<languages.length; i++) {
            var object = {}
            object.id = languages[i].iso_code
            object.text = languages[i].name_en
            languageJson.push(object)
        }
        redis.set('languages', JSON.stringify(languageJson))
    })
    .catch(function (err) {
        throw err
    })
}

根据@Bergi 更新

var promise = Language.findOne({}).exec()

promise.then(function(languages) {
    /* If collection is empty initialize it */
    if(!languages)
        insertLanguagesIntoMongoDB()
     return languages
})
.then(function(languages) {
    /* Make sure it's cached in Redis */
    return insertLanguagesIntoRedis(languages)
});

function insertLanguagesIntoRedis(languages) {
    logger.info("Inserting languages into redis db")
    var promise = Language.find({}).exec()
    return promise.then(function(languages) {
        if(!languages)
            throw new Error("Couldn't find any languages in MongoDB's Language collection")

        var languageJson = []
        for(var i=0; i<languages.length; i++) {
            var object = {}
            object.id = languages[i].iso_code
            object.text = languages[i].name_en
            languageJson.push(object)
        }
        redis.set('languages', JSON.stringify(languageJson))
    })
}