Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建对象的对象_Javascript_Asynchronous_Promise - Fatal编程技术网

Javascript 创建对象的对象

Javascript 创建对象的对象,javascript,asynchronous,promise,Javascript,Asynchronous,Promise,我试图在JS中创建一个对象对象,但我认为异步执行存在一些问题 这是我的密码: //从.docx提取数据(剧集号、字符名)到JSON var猛犸象=需要(“猛犸象”) 函数docx2Object(文件路径){ 返回mammoth.extractRawText({path:filepath}) .然后(功能(res){ 返回res.value; }) .然后(功能(res){ 设arr={} arr[“number”]=“”+res.match(/(?!插曲#))\d+/) arr[“names”

我试图在JS中创建一个对象对象,但我认为异步执行存在一些问题

这是我的密码:

//从.docx提取数据(剧集号、字符名)到JSON
var猛犸象=需要(“猛犸象”)
函数docx2Object(文件路径){
返回mammoth.extractRawText({path:filepath})
.然后(功能(res){
返回res.value;
})
.然后(功能(res){
设arr={}
arr[“number”]=“”+res.match(/(?!插曲#))\d+/)
arr[“names”]=[…新集合(res.match(/^[^:\r\n]+(?=:)/gm))]
返回arr
})
}
函数files2JSON(arrayscript){
设arr={}
设i=0
forEach(函数(元素){
docx2Object(元素)。then(函数(res){
arr[i++]=res
log(JSON.stringify(arr,null,'\t'))
})
})
返回JSON.stringify(arr,null,“\t”)
}
让ArrayScript=[“doc1.docx”,“doc2.docx”]

log(files2JSON(arrayscript))
我认为您的问题在于

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,然后看看在调试异步代码时会执行什么。这确实有助于了解执行顺序


这将有助于获取输出。因为您使用的是全局错误的增量计数以及包含错误代码的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));

您是否尝试过调试它,并在then函数中放入了大量控制台日志。并查看哪些执行,哪些不执行,以及对象发生了什么。如果我错了,请告诉我,但您不能有多个退货,因为第一个退货将取消以下所有退货