如何将所有这些节点代码转换为 promise
How to convert all this node code to promise
我使用下面的代码,我想将它转换为 promise,所以我已经开始 promisify 子进程,但我的问题是如何将所有转换为 promise,
我需要将代码划分为其他(见评论)方法
当我按原样使用代码时,它的工作正常。
var Promise = require('bluebird'),
fs = require('fs').
module.exports = {
createNew: function () {
fs.readFile("c://test.txt", 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
// 1. from Here to needed to divide to new method
// which just return cmd value
var fileKeyValObj = {};
data.split("\n").forEach(function (element) {
var sep = element.indexOf(':');
});
var cmd = fileKeyValObj['name'];
// 2. this code should be in additional method
if (typeof cmd !== 'undefined') {
var exec = Promise.promisify(require('child_process').exec);
var childProcess = exec(cmd).spread(function (stdout, stderr) {
}).catch(function (error) {
console.log(error)
});
}
});
},
}
您可以在 fs 上使用 bluebird .promisifyAll()
函数以及您的 exec 命令,然后将它们链接在一起:
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var exec = Promise.promisify(require('child_process').exec);
var parseFile = function(data) {
var fileKeyValObj = {};
data.split('\n').forEach(function (element) {
var sep = element.indexOf(':');
var cmdValue = element.substring(sep + 2);
fileKeyValObj[element.substr(3, sep)] = cmdValue;
});
return fileKeyValObj['name'];
};
module.exports = {
createNew: function () {
return fs.readFileAsync('c://test.txt', 'utf8')
.then(parseFile)
.then(exec)
.spread(function(stdout, stderr) {
// do stuff here with stdout and stderr
if (stderr) {
throw new Error('An Error Occurred.');
}
// anything returned will be returned on the next item in the chain.
return stdout;
});
},
};
如何从另一个模块调用函数:
var myModule = require('./path/to/module');
myModule.createNew().then(function(data) {
// this is the data returned from the final step of the createNew promise chain.
console.log(data);
}).catch(function(err) {
// catch and handle errors here instead.
console.error(err);
});
您应该在 fs
上使用 Promise.promisifyAll
。
import Promise from 'bluebird'
let fs = Promise.promisifyAll(require('fs'))
let exec = Promise.promisify(require('child_process').exec)
function mapCommands (file) {
return file.split('\n').reduce((p, c) => {
let separation = c.indexOf(':')
p[c.slice(3, separation)] = c.slice(separation + 2)
return p
}, {})
}
function execCommands (obj) {
let commands = []
for (let key in obj) {
let cmd = exec(obj[key])
commands.push(cmd)
}
return Promise.all(commands)
}
export default {
createNew () {
fs.readFileAsync('C://test.txt', 'utf8')
.then(mapCommands)
.then(execCommands)
.then(() => {
// success; all commands executed
}).catch((err) => {
console.log(err) // handle err
})
}
}
我使用下面的代码,我想将它转换为 promise,所以我已经开始 promisify 子进程,但我的问题是如何将所有转换为 promise, 我需要将代码划分为其他(见评论)方法 当我按原样使用代码时,它的工作正常。
var Promise = require('bluebird'),
fs = require('fs').
module.exports = {
createNew: function () {
fs.readFile("c://test.txt", 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
// 1. from Here to needed to divide to new method
// which just return cmd value
var fileKeyValObj = {};
data.split("\n").forEach(function (element) {
var sep = element.indexOf(':');
});
var cmd = fileKeyValObj['name'];
// 2. this code should be in additional method
if (typeof cmd !== 'undefined') {
var exec = Promise.promisify(require('child_process').exec);
var childProcess = exec(cmd).spread(function (stdout, stderr) {
}).catch(function (error) {
console.log(error)
});
}
});
},
}
您可以在 fs 上使用 bluebird .promisifyAll()
函数以及您的 exec 命令,然后将它们链接在一起:
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var exec = Promise.promisify(require('child_process').exec);
var parseFile = function(data) {
var fileKeyValObj = {};
data.split('\n').forEach(function (element) {
var sep = element.indexOf(':');
var cmdValue = element.substring(sep + 2);
fileKeyValObj[element.substr(3, sep)] = cmdValue;
});
return fileKeyValObj['name'];
};
module.exports = {
createNew: function () {
return fs.readFileAsync('c://test.txt', 'utf8')
.then(parseFile)
.then(exec)
.spread(function(stdout, stderr) {
// do stuff here with stdout and stderr
if (stderr) {
throw new Error('An Error Occurred.');
}
// anything returned will be returned on the next item in the chain.
return stdout;
});
},
};
如何从另一个模块调用函数:
var myModule = require('./path/to/module');
myModule.createNew().then(function(data) {
// this is the data returned from the final step of the createNew promise chain.
console.log(data);
}).catch(function(err) {
// catch and handle errors here instead.
console.error(err);
});
您应该在 fs
上使用 Promise.promisifyAll
。
import Promise from 'bluebird'
let fs = Promise.promisifyAll(require('fs'))
let exec = Promise.promisify(require('child_process').exec)
function mapCommands (file) {
return file.split('\n').reduce((p, c) => {
let separation = c.indexOf(':')
p[c.slice(3, separation)] = c.slice(separation + 2)
return p
}, {})
}
function execCommands (obj) {
let commands = []
for (let key in obj) {
let cmd = exec(obj[key])
commands.push(cmd)
}
return Promise.all(commands)
}
export default {
createNew () {
fs.readFileAsync('C://test.txt', 'utf8')
.then(mapCommands)
.then(execCommands)
.then(() => {
// success; all commands executed
}).catch((err) => {
console.log(err) // handle err
})
}
}