我正在尝试在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"
]
}
}
我的数组是空的,我的索引不匹配。任何人都可以帮忙吗?
答案 0 :(得分:1)
我认为你的问题在于
arrayScripts.forEach(function(element) {
docx2Object(element).then(function (res) {
arr[i++] = res
console.log(JSON.stringify(arr, null, '\t'))
})
})
docx2Object
调用是异步的,意味着forEach
的迭代将立即返回并执行。这意味着files2JSON可能在所有处理完成之前执行。
您最想做的就是将每个promise存储到一个数组中,然后使用Promise.all()
一次解决所有这些问题并从Promise.all()
返回promise并等待它。总的来说,这个结构看起来像
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
并看看什么时候执行。它确实有助于查看执行顺序。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
答案 1 :(得分:0)
这将有助于获取输出。当您使用全局错误的增量计数以及包含错误代码的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));