Node Promise return 对象,其值为 resolved promise
Node Promise return object with value of resolved promise
我在下面有一些代码 return 目录中的所有文件都具有特定的键和值。我想成为具有布尔值的 isDirectory 的其中一个键。
下面的代码工作得很好,但我想知道是否有办法删除 Promise。all/iterating over promises 而是将 stat.isDirectory()
的解析值推送到我的文件对象直接在我的地图上。
我尝试但失败的解决方案:
我试过这样的事情:
isDirectory: fs.statAsync(path).then((stat) => stat.isDirectory())
然后在所有 isDirectory
键上执行 Promise.all
工作代码:
const Promise = require('bluebird'),
os = require('os'),
_ = require('lodash'),
fs = Promise.promisifyAll(require('fs')),
desktopPath = `${os.homedir()}/Desktop`;
let files = [];
return fs.readdirAsync(desktopPath)
.then((filesName) => files = _.map(filesName, (fileName) => ({
path: `${desktopPath}/${fileName}`,
name: fileName,
ext: _.last(fileName.split('.'))
})))
.then(() => Promise.all(_.map(files, (file) => fs.statAsync(file.path))))
.then((stats) => _.forEach(stats, (stat, idx) => {
files[idx].isDirectory = stat.isDirectory();
}))
.then(() => {
console.log(files);
})
有没有办法去掉最后的Promise.all和_.forEach部分?而不是在我的地图上执行这些操作?
假设您使用的是最新的 Node - 您可以 async/await
,将 async
放在函数定义之前并执行:
const fileNames = await fs.readdirAsync(desktopPath);
const files = await Promise.map(fileNames, fileName => Promise.props({
path: `${desktopPath}/${fileName}`,
name: fileName,
ext: fileName.split('.').pop(),
isDirectory: fs.statAsync(`${desktopPath}/${fileName}`);
})));
console.log(files);
return files;
您不能完全删除 Promise.all
,因为您希望在使用最终结果之前等待所有文件完成。但是您可以在一次 .then()
调用中完成所有操作。
因为 map
是同步的,它不会等待 fs.statAsync
完成。但是你可以创建一个 fs.statAsync
的 promises 数组,用最终的文件对象解析,然后使用 Promise.all
.
简单地等待它们全部完成
详细版本,包含一些注释以供澄清:
fs.readdirAsync(desktopPath)
.then(fileNames => {
// Array of promises for fs.statAsync
const statPromises = fileNames.map(fileName => {
const path = `${desktopPath}/${fileName}`;
// Return the final file objects in the promise
return fs.statAsync(path).then(stat => ({
path,
name: fileName,
ext: _.last(fileName.split(".")),
isDirectory: stat.isDirectory()
}));
});
// Promise.all to wait for all files to finish
return Promise.all(statPromises);
})
.then(finalFiles => console.log(finalFiles));
精简版:
fs.readdirAsync(desktopPath)
.then(fileNames => Promise.all(
fileNames.map(fileName =>
fs.statAsync(`${desktopPath}/${fileName}`).then(stat => ({
path: `${desktopPath}/${fileName}`,
name: fileName,
ext: _.last(fileName.split(".")),
isDirectory: stat.isDirectory()
})))
))
.then(finalFiles => console.log(finalFiles));
我在下面有一些代码 return 目录中的所有文件都具有特定的键和值。我想成为具有布尔值的 isDirectory 的其中一个键。
下面的代码工作得很好,但我想知道是否有办法删除 Promise。all/iterating over promises 而是将 stat.isDirectory()
的解析值推送到我的文件对象直接在我的地图上。
我尝试但失败的解决方案:
我试过这样的事情:
isDirectory: fs.statAsync(path).then((stat) => stat.isDirectory())
然后在所有 isDirectory
键上执行 Promise.all
工作代码:
const Promise = require('bluebird'),
os = require('os'),
_ = require('lodash'),
fs = Promise.promisifyAll(require('fs')),
desktopPath = `${os.homedir()}/Desktop`;
let files = [];
return fs.readdirAsync(desktopPath)
.then((filesName) => files = _.map(filesName, (fileName) => ({
path: `${desktopPath}/${fileName}`,
name: fileName,
ext: _.last(fileName.split('.'))
})))
.then(() => Promise.all(_.map(files, (file) => fs.statAsync(file.path))))
.then((stats) => _.forEach(stats, (stat, idx) => {
files[idx].isDirectory = stat.isDirectory();
}))
.then(() => {
console.log(files);
})
有没有办法去掉最后的Promise.all和_.forEach部分?而不是在我的地图上执行这些操作?
假设您使用的是最新的 Node - 您可以 async/await
,将 async
放在函数定义之前并执行:
const fileNames = await fs.readdirAsync(desktopPath);
const files = await Promise.map(fileNames, fileName => Promise.props({
path: `${desktopPath}/${fileName}`,
name: fileName,
ext: fileName.split('.').pop(),
isDirectory: fs.statAsync(`${desktopPath}/${fileName}`);
})));
console.log(files);
return files;
您不能完全删除 Promise.all
,因为您希望在使用最终结果之前等待所有文件完成。但是您可以在一次 .then()
调用中完成所有操作。
因为 map
是同步的,它不会等待 fs.statAsync
完成。但是你可以创建一个 fs.statAsync
的 promises 数组,用最终的文件对象解析,然后使用 Promise.all
.
详细版本,包含一些注释以供澄清:
fs.readdirAsync(desktopPath)
.then(fileNames => {
// Array of promises for fs.statAsync
const statPromises = fileNames.map(fileName => {
const path = `${desktopPath}/${fileName}`;
// Return the final file objects in the promise
return fs.statAsync(path).then(stat => ({
path,
name: fileName,
ext: _.last(fileName.split(".")),
isDirectory: stat.isDirectory()
}));
});
// Promise.all to wait for all files to finish
return Promise.all(statPromises);
})
.then(finalFiles => console.log(finalFiles));
精简版:
fs.readdirAsync(desktopPath)
.then(fileNames => Promise.all(
fileNames.map(fileName =>
fs.statAsync(`${desktopPath}/${fileName}`).then(stat => ({
path: `${desktopPath}/${fileName}`,
name: fileName,
ext: _.last(fileName.split(".")),
isDirectory: stat.isDirectory()
})))
))
.then(finalFiles => console.log(finalFiles));