【发布时间】:2017-02-08 13:35:51
【问题描述】:
我正在使用fs 和phantomJS
const phantom = require('phantom');
const fs = require('fs');
我有 4 条路线(网址)从 phantom JS 打开。打开时,会读取页面内容,然后node.fs 会将该内容写入它自己的 html 文件中。
const routes = [
'about',
'home',
'todo',
'lazy',
]
问题:
我如何为const routes 中的每个值并行循环这个异步函数。
(async function() {
const instance = await phantom.create();
const page = await instance.createPage();
const status = await page.open(`http://localhost:3000/${routes}`);
const content = await page.property('content');
await fsPromise(`${routes}.html`, content);
await instance.exit();
}());
const fsPromise = (file, str) => {
return new Promise((resolve, reject) => {
fs.writeFile(file, str, function (err) {
if (err) return reject(err);
resolve(`${routes} > ${routes}.html`);
});
})
};
【问题讨论】:
标签: node.js asynchronous async-await phantomjs