2016-12-01 30 views
0

我有以下的輔助方法:產量與promisified READFILE返回undefined

module.exports.rootUrl = co(function*() { 
    const jsonPath = path.resolve(process.cwd(), './server/config.json'); 

    console.log(jsonPath); 
    let config; 
    try { 
    const readFile = Promise.promisify(fs.readFile); 
    const config = yield readFile(jsonPath, 'utf-8'); 
    } catch (err) { 
    console.dir(err); 
    yield Promise.reject(err); 
    }; 

    console.dir(config); 

    const url = `http://${config.host}:${config.port}/${config.restApiRoot}`; 

    console.log(url); 

    yield Promise.resolve(url); 
}) 

的問題是,調用yield. readFile(jsonPath, 'utf-8')被返回undefined。

回答

1

的問題是,你重新聲明config(如塊範圍的const)的try內,這意味着它是超出範圍時你console.dir它,你最終記錄你聲明的(不確定)configlet

這應該工作:

config = yield readFile(jsonPath, 'utf-8');