kentor
kentor

Reputation: 18514

Call functions in bluebird's then

How can I call a function inside of a then()? I am new to Bluebird and previously I would simply trigger the callback to inform that the called function is done with it's stuff.

Here is my "main code" which inserts locale data into my Mongo and Redis database.

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
})

This is how my function looks like, but it says "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
    })
}

So how would I properly define and call my insertLanguagesIntoRedis and insertLanguagesIntoMongoDB functions?

Upvotes: 0

Views: 88

Answers (1)

akaphenom
akaphenom

Reputation: 6888

Something along these lines might work:

  1. pass the languages into the promise chain
  2. at the end of the chain return the promises into the next step
  3. adjust the next step to accept the languages
  4. adjust the insertLanguagesIntoRedis to accept the languages
  5. have insertLanguagesIntoRedis return the promise chain
  6. remove the callback invocation from 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
    })
}

update per @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))
    })
}

Upvotes: 1

Related Questions