2017-07-28 61 views
1

無法弄清楚爲什麼'describeDir'諾言鏈不能接受..任何人有一個想法,我在這裏搞砸了嗎?所有的代碼似乎都會執行,但是任何承諾api函數,例如那麼或最後永遠不會執行。下面顯示了兩個頂級功能。 github回購點位於https://github.com/PhoenixContactUSA/pcworx-doc-genPromise chain then then-able

function updateDescriptor(fileloc, wsName, outdir){ 
    console.log('Updating descriptor file for: ' + wsName); 
    return new Promise(function(resolve, reject){ 
    return getDescriptor(outdir).then(
     (value) => { 
     let descriptorFile = value; 

     var comments = getComments(fileloc); 
     var variables = getVariables(fileloc); 

     //wait until both are completed before continuing 
     return Promise.all([comments, variables]).then((values) => { 
      //var descriptor = new Object(); 
      //console.log(JSON.stringify(descriptor)); 
      descriptorFile[wsName] = new Object(); 
      //console.log(JSON.stringify(descriptor)); 

      //var worksheet = new Object(); 
      descriptorFile[wsName].comments = values[0]; 
      descriptorFile[wsName].variables = values[1]; 

      //save the file 
      return saveDescriptor(descriptorFile, outdir).then((value) => { 
      console.log('Completed ' + wsName + ' ' + value); 
      resolve(value); 
      }, (reason) => {console.log(reason)}) 

     }, (reason) => { 
      console.log(reason); 
     } 

     ) 


     }, 
     (reason) => {console.log(reason)} 
    ) 



    }) 



} 


function describeDir(filedir, outdir){ 

    var files = findFilesInDir(filedir, '.XML'); 
    for (var k=0;k<files.length;k++){ 
    if ((files[k].indexOf('@HW') !== -1) || (files[k].indexOf('@LIBS') !== -1) || (files[k].indexOf('@ROOT') !== -1) || (files[k].indexOf('run') !== -1)) { 
     files.splice(k,1); 
    } 
    } 

    return Promise.each(files, function(file){ 
     return updateDescriptor(file, path.basename(file), outdir); 
    }); 

} 

然後我在這裏調用函數。代碼似乎執行得很好,但是從不調用then()。請注意,我在這個最新版本中使用了藍鳥。

//generate the output files, then copy them to the destination 
    docProcessor.describeDir(folder, path.join(__dirname, '..')).then((value)=>{ 
     console.log('docProcessor then entered: ' + value); 
    }); 
+0

您可以包括或指明對您正試圖'then'承諾鏈條行? – TheHansinator

+0

您是否收到任何錯誤? –

+0

沒有錯誤。它是基於節點的,一半的問題是我目前沒有得到任何調試信息。 – zmink1

回答

1

首先,要檢查是否有拒絕,嘗試

docProcessor.describeDir(folder, path.join(__dirname, '..')) 
.then(value => console.log('docProcessor then entered:', value)) 
.catch(reason => console.error('error', reason); 

describeDir一個潛在的問題是,您使用的名稱與@HW@LIBS@ROOTrun過濾掉的文件的循環

當您拼接k上的文件陣列時,k++仍會執行,因此您將跳過測試下一個文件

array = [a, b, c, d]; 
k == 1 // testing "b" 
array.splice(k, 1); 
now array = [a, c, d] 
k++; // == 2 
next iteration checks "d" 

因此,如果曾經有與他們在這些字符串中的一個排的兩個文件,你會跳過「刪除」了 - 這可能是這個問題?

您要使用過濾器,而不是

function describeDir(filedir, outdir) { 
    var files = findFilesInDir(filedir, '.XML') 
    .filter(file => 
     file.indexOf('@HW') == -1 && 
     file.indexOf('@LIBS') == -1 && 
     file.indexOf('@ROOT') == -1 && 
     file.indexOf('run') == -1 
    ); 

    return Promise.each(files, file => updateDescriptor(file, path.basename(file), outdir)); 
} 

或整潔

function describeDir(filedir, outdir) { 
    var files = findFilesInDir(filedir, '.XML') 
    .filter(file => !/@HW|@LIBS|@ROOT|run/.test(file)); 

    return Promise.each(files, file => updateDescriptor(file, path.basename(file), outdir)); 
} 

作爲獎勵,以下是updateDescriptor功能清理和扁平出來,並與最新的ES2015 +編碼功能(與現代化您的意見完好無損)

function updateDescriptor(fileloc, wsName, outdir) { 
    console.log('Updating descriptor file for: ' + wsName); 
    return getDescriptor(outdir) 
    .then(value => Promise.all([getComments(fileloc), getVariables(fileloc), value])) 
    .then(([comments, variables, descriptorFile]) => { 
     //var descriptor = new Object(); 
     //console.log(JSON.stringify(descriptor)); 
     //console.log(JSON.stringify(descriptor)); 

     //descriptorFile[wsName] = new Object(); 

     //var worksheet = new Object(); 
     descriptorFile[wsName] = {comments, variables}; 
     //save the file 
     return saveDescriptor(descriptorFile, outdir) 
    }).then((value) => { 
     console.log('Completed ' + wsName + ' ' + value); 
     return value 
    }) 
} 

注意缺乏catch代碼,只要你想的錯誤,堅持下來的鏈條

updateDescriptor一個真正的緊湊型是

const updateDescriptor = (fileloc, wsName, outdir) => getDescriptor(outdir) 
    .then(value => Promise.all([getComments(fileloc), getVariables(fileloc), value])) 
    .then(([comments, variables, descriptorFile]) => 
     saveDescriptor(Object.assign(descriptorFile, { 
      [wsName] : { comments, variables } 
     }), outdir) 
    ); 
+0

感謝您的反饋,這對我的代碼有很大的改進。相信與否,當describeDir被調用時,我仍然沒有抓住或執行,儘管代碼仍然被執行。也許我的代碼中有更多的節點正在吃點東西?在發佈更有用的東西之前,我會繼續調查 – zmink1