2014-07-14 47 views
0

任何人都可以解釋下面的代碼中callback()函數做什麼嗎?這個回調函數來自哪裏?作爲模塊的回調函數。導出參數

index.js

var _ = require('lodash'); 
var i18n = require('./lib/i18n'); 

var options = { 
    stage: 'options:post:configuration' 
}; 

/** 
* postprocess 
* @param {Object} params 
* @param {Function} callback 
*/ 
module.exports = function (params, callback) { 
    'use strict'; 

    var grunt = params.grunt; 

    grunt.verbose.subhead('Running:'.bold, '"assemble-contrib-i18n"'); 
    grunt.verbose.writeln('Stage: '.bold, '"' + params.stage + '"\n'); 

    var opts = params.assemble.options.i18n; 
    grunt.verbose.writeln('Options: '.bold, require('util').inspect(opts)); 

    if (opts) { 
    var o = {}; 
    var data = opts.data; 
    var templates = opts.templates; 
    var languages = opts.languages; 

    if (templates) { 
     o.templates = templates; 
    } 

    if (languages) { 
     o.languages = languages; 
    } 
//data = path to a json file containing a list of languages, o = templates & languages 
    var pages = i18n(data, o); 

    grunt.verbose.writeln('Pages: '.bold, require('util').inspect(pages)); 
    params.assemble.options.pages = _.extend({}, (params.assemble.options.pages || {}), pages); 

    } 

    callback(); 
}; 

module.exports.options = options; 

和地方被使用了這個index.js文件在這個文件中gruntfile.js

"with-plugin": { 
    options: { 
     plugins: ['./index.js'], 
     i18n: { 
     data: ['test/fixtures/data/i18n.json'], 
     templates: ['test/fixtures/templates/*.hbs'] 
     } 
    }, 
    dest: 'test/actual/with-plugin/', 
    src: '!*.*' 
    }, 

有啥這裏是回調函數?

回答

0

定義的函數(將回調作爲參數接受)是當此模塊的腳本requires時導入的值。因此,回調被調用腳本提供:

require('someModule')(someParams, myCallback); 
// which is the same as: 
var func = require('someModule'); 
func(someParams, myCallback); 

在您所提供的代碼,回調沒有什麼特別之處,它只是在函數結束時執行。

+0

嗨,我已更新我的帖子。你能詳細闡述一下在這種情況下什麼是回調函數? – Blake