我在 node.js 中有一个批处理作业: 将文件复制到目录中,对文件进行分析,然后删除文件。
我想迭代一系列作业并使用生成器暂停执行,直到批处理作业完成,然后再开始另一个作业。这是我到目前为止所拥有的:
const cars = ["toyota", "honda", "acura"];
function copyFilesAndRunAnalysis(car) {
return new Promise(function(resolve, reject) {
setTimeout(function() { // simulate some delay
resolve(); // control should return to generator here
}, 1000);
});
}
function* doCar(car) {
yield copyFilesAndRunAnalysis(car);
}
// BEGIN HERE
console.log('start here');
carBatch = doCar(cars[0]);
carBatch.next(); // confusion here!!!
carBatch.next(); // should this all be in a forEach loop?Run Code Online (Sandbox Code Playgroud)
我想做的是有一个 forEach 循环每辆车,完成该copyFilesAndRunAnalysis方法中的所有相应工作 - 暂停直到Promise.resolve()然后继续到下一辆。尝试 forEach 根本不会让任何东西运行。
我的节点进程快要死了,我似乎无法在进程退出时登录到文件。这是一个长时间运行的进程,直接调用node index.js:
// index.js
const fs = require('fs');
exports.getAllCars = (process => {
if (require.main === module) {
console.log(`Running process: ${process.getgid()}.`);
let out = fs.createWriteStream(`${__dirname}/process.log`);
// trying to handle process events here:
process.on('exit', code => out.write(`Exit: ${code}`));
return require('./lib/cars').getAllCars();
} else {
return require('./lib/cars').getAllCars;
}
})(process);
Run Code Online (Sandbox Code Playgroud)
还尝试为error,创建事件处理程序uncaughtException。手动终止我的进程时没有任何作用(使用kill {pid})。该文件process.log已创建,但没有任何内容。可写流是否需要stream.end()在完成时调用?
我有以下网址:
http://website.com/testing/test2/
我想删除最后两个斜杠之间的文本,这样会产生:
http://website.com/testing/
我正在将一些旧节点模块重构为更实用的样式.对于FP来说,我就像第二年的新生一样.我一直在忙着处理大型异步流程.这是一个示例,我正在向db发出请求,然后缓存响应:
// Some external xhr/promise lib
const fetchFromDb = make => {
return new Promise(resolve => {
console.log('Simulate async db request...'); // just simulating a async request/response here.
setTimeout(() => {
console.log('Simulate db response...');
resolve({ make: 'toyota', data: 'stuff' });
}, 100);
});
};
// memoized fn
// this caches the response to getCarData(x) so that whenever it is invoked with 'x' again, the same response gets returned.
const getCarData = R.memoizeWith(R.identity, (carMake, response) => response.data);
// Is this function …Run Code Online (Sandbox Code Playgroud)javascript asynchronous functional-programming node.js ramda.js
javascript ×3
node.js ×3
asynchronous ×1
ecmascript-6 ×1
es6-promise ×1
generator ×1
ramda.js ×1