2017-08-30 234 views
0

我想在JS中創建對象的對象,但我認爲我有一些異步執行的問題。創建對象的對象

這裏是我的代碼:

// Extract data (episode number, characters firstname's) from .docx to JSON 
 

 
var mammoth = require("mammoth") 
 

 
function docx2Object (filepath) { 
 
    
 
    return mammoth.extractRawText({path: filepath}) 
 
    .then(function (res) { 
 
     return res.value; 
 
    }) 
 
    .then(function (res) { 
 
     let arr = {} 
 
     arr["number"] = "" + res.match(/(?!Episode #)\d+/) 
 
     arr["names"] = [...new Set(res.match(/^[^:\r\n]+(?=:)/gm))] 
 
     return arr 
 
    }) 
 
} 
 

 
function files2JSON (arrayScripts) { 
 
    
 
    let arr = {} 
 
    let i = 0 
 
    arrayScripts.forEach(function(element) { 
 
     docx2Object(element).then(function (res) { 
 
      arr[i++] = res 
 
      console.log(JSON.stringify(arr, null, '\t')) 
 
     }) 
 
    }) 
 
    return JSON.stringify(arr, null, '\t') 
 
} 
 

 
let arrayScripts = ["doc1.docx", "doc2.docx"] 
 
console.log(files2JSON(arrayScripts))

這裏是輸出:

{} 

{ 
    "0": { 
     "number": "10600", 
     "names": [ 
      "Hilary", 
      "Jean-Claude", 
      "Devon", 
      "Jean Claude", 
      "Cane", 
      "Lily", 
      "Jack", 
      "Phyllis", 
      "Victor", 
      "Nikki", 
      "Neil", 
      "Paul", 
      "Dr. Barrett", 
      "Christine", 
      "Kelly" 
     ] 
    } 
} 
{ 
    "0": { 
     "number": "10600", 
     "names": [ 
      "Hilary", 
      "Jean-Claude", 
      "Devon", 
      "Jean Claude", 
      "Cane", 
      "Lily", 
      "Jack", 
      "Phyllis", 
      "Victor", 
      "Nikki", 
      "Neil", 
      "Paul", 
      "Dr. Barrett", 
      "Christine", 
      "Kelly" 
     ] 
    }, 
    "1": { 
     "number": "10601", 
     "names": [ 
      "Hilary", 
      "Devon", 
      "Jean+Claude", 
      "Jean + Claude", 
      "Jean/Claude", 
      "Jean/Claude", 
      "Cane", 
      "Lily", 
      "Jack", 
      "Phyllis", 
      "Victor", 
      "Nikki", 
      "Neil", 
      "Paul", 
      "Dr. Barrett", 
      "Christine", 
      "Kelly" 
     ] 
    } 
} 

我的數組是空的,我的索引不match.Can誰能幫助?

+0

你試過調試它,把一堆控制檯日誌放到你的函數中。並查看哪些執行,哪些不執行和您的對象發生了什麼。並告訴我,如果我錯了,但你不能有一個以上的回報,因爲第一個將取消一切波紋 – noitse

回答

1

我覺得你的問題是與

arrayScripts.forEach(function(element) { 
    docx2Object(element).then(function (res) { 
     arr[i++] = res 
     console.log(JSON.stringify(arr, null, '\t')) 
    }) 
}) 

docx2Object呼叫在forEach的異步含義迭代將返回,並立即執行。這意味着files2JSON可能會在所有處理完成之前執行。

最有可能的是將每個承諾存儲到一個數組中,然後使用Promise.all()一次性解決它們,並從Promise.all()返回承諾並等待它。總體來說這個結構會看起來像

function asyncTask1(task) { 
    return getPromise(task); 
} 

function asyncTask2() { 
    var tasks = ["eat", "sleep"]; 
    var promises = tasks.map(function(task) { 
    return asyncTask1(task); 
    }); 
    return Promise.all(promises); 
} 

asyncTask2().then(function(response) { 
    console.log("I ate and slept."); 
}); 

一件事調試異步代碼只是拋出一大堆console.log S和看什麼時候被執行時,我親自做的。它確實有助於看到執行順序。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

+0

非常感謝,它的工作! – amerej

0

這將有助於讓你的output.As你使用全局錯誤的增量次數也含有錯誤代碼mammoth.extractRawText()方法。

// Extract data (episode number, characters firstname's) from .docx to JSON 
var mammoth = require("mammoth"); 

function docx2Object (filepath) { 

    return mammoth.extractRawText({path: filepath}) 
     .then(function (res) { 
     return res.value; 
     }).then(function (res) { 
     var arr = {}; 
     arr["number"] = "" + res.match(/(?!Episode #)\d+/); 
     //arr["names"] = [...new Set(res.match(/^[^:\r\n]+(?=:)/gm))]; 
     return arr 
     }); 

} 

function files2JSON (arrayScripts) { 
    var arr = {}; 

    arrayScripts.forEach(function(element,index) { 
    docx2Object(element).then(function (res) { 
     arr[index++] = res; 
     console.log(res,index) 
     console.log(JSON.stringify(arr, null, '\t')); 
    }) 
    }); 
    return JSON.stringify(arr, null, '\t'); 
} 

var arrayScripts = ["doc1.docx", "doc2.docx"]; 
console.log(files2JSON(arrayScripts));