【问题标题】:How can i get the content from a promise object?? NodeJs如何从 Promise 对象中获取内容? NodeJs
【发布时间】:2019-02-13 20:43:22
【问题描述】:

我想获取一个文件夹的内容并调用这个命令->“sftp.list (...)”。

但这会返回一个 Promise 对象。 我怎样才能得到这个内容的名字??

我在下面附上了我的代码,但我总是只取回所有信息。

var a = sftp.list(remoteFilename);
a.then(function(result){
   console.log(a);
});


OUTPUT:
  { type: '-',
    name: '14335.JSON',
    size: 482369,
    modifyTime: 1549637889000,
    accessTime: 1549541207000,
    rights: { user: 'rw', group: 'rw', other: 'rw' },
    owner: 98438,
    group: 840223400513 } ]

但我只想要文件中的名称。

【问题讨论】:

标签: javascript node.js promise


【解决方案1】:

您的代码不正确,要返回Promise 的结果,您必须使用return,如下所示:

var a = new Promise(function(res, rej){
                    // return promise
                    return sftp.list(remoteFilename);
                }).then((result) => {console.log(result); // your result here});

如果需要获取a的返回值,使用await

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2019-03-27
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 2023-03-16
    • 2011-12-01
    相关资源
    最近更新 更多