Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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 Node.js可以';不履行承诺(承诺{<;待定>;})_Javascript_Node.js_Es6 Promise - Fatal编程技术网

Javascript Node.js可以';不履行承诺(承诺{<;待定>;})

Javascript Node.js可以';不履行承诺(承诺{<;待定>;}),javascript,node.js,es6-promise,Javascript,Node.js,Es6 Promise,我是异步编码新手 我正在使用csvtojson库,试图转换一个csv文件并将结果传递到另一个模块 convert()函数如下所示: convert: function (csvFilePath) { return new Promise((resolve, reject) => { const options = { delimiter: ["|",","], noHeader: true ,

我是异步编码新手

我正在使用csvtojson库,试图转换一个csv文件并将结果传递到另一个模块

convert()函数如下所示:

convert: function (csvFilePath) {
  return new Promise((resolve, reject) => {

    const options = { delimiter: ["|",","],
                      noHeader: true ,
                      headers: ["header1", "header2"]
                    }

    csv(options)
    .fromFile(csvFilePath)
    .on('end_parsed',(convertedJson) => {
      resolve(convertedJson);
    })
    .on("done",(error) => {
      reject(error);
    })
  });
}
我的电话:

const converter = require ("./converter")();

let json;
json = converter.convert("./importSample.csv");
console.log(json);
当我执行代码时,我可以看到承诺仍然处于挂起状态:

Promise{}


我想我必须使用
.then()
函数,但我不知道在哪里或如何使用。

转换器
函数中,您得到了承诺,并且该对象具有方法
then
。你应该这样做

const converter = require ("./converter")();

converter.convert("./importSample.csv").then(json => {
  console.log(json);
}).catch(error => {
  console.log(error);
});

你可以找到很好的关于承诺的教程,这是承诺的文档。

转换器
函数中,你可以得到承诺,并且该对象有方法
,然后
。你应该这样做

const converter = require ("./converter")();

converter.convert("./importSample.csv").then(json => {
  console.log(json);
}).catch(error => {
  console.log(error);
});

您可以找到关于承诺的不错的教程,这是承诺的文档。

承诺有一个固定的语法体系结构。我将用一个简单的代码来解释它

var x = new Promise((resolve,reject)=>{
    //here you perform an asynchronous call
    resolve(value); //receive it in 'then' part of promise
    reject(error): //if your operation fails due to any error, you call reject, which is handled by 'catch' part of the promise.
});
x.then((value)=>{
    //this is the part which was called using resolve, and the value it receives is the value you passed as argument in resolve.
});
x.catch((error)=>{
    //this part is called by reject. the error received is the value you passed inside the reject.
});
因此,您的函数应该类似于-

converter.convert("./importSample.csv").then((json)=>{
    //here is your part of code which is to be handled synchronously after the promise is called.
}).catch((error)=>{
     //in case any error occurs and you want your program to exit gracefully.
});

Promise有一个固定的语法结构。我将用一个简单的代码来解释它

var x = new Promise((resolve,reject)=>{
    //here you perform an asynchronous call
    resolve(value); //receive it in 'then' part of promise
    reject(error): //if your operation fails due to any error, you call reject, which is handled by 'catch' part of the promise.
});
x.then((value)=>{
    //this is the part which was called using resolve, and the value it receives is the value you passed as argument in resolve.
});
x.catch((error)=>{
    //this part is called by reject. the error received is the value you passed inside the reject.
});
因此,您的函数应该类似于-

converter.convert("./importSample.csv").then((json)=>{
    //here is your part of code which is to be handled synchronously after the promise is called.
}).catch((error)=>{
     //in case any error occurs and you want your program to exit gracefully.
});