异步问题,无法在 .then 之后执行 运行 shelljs 命令
async issue, unable to run shelljs commands after .then
更新:我在写入文件后添加了一个短暂的暂停,直到那时 shell 命令才起作用。
async function testRun() {
await createInsertCurlReq.createInsertCurlReq(process.argv[2]);
await timeout(3000);
shell.chmod("+x", "./curl.sh");
shell.exec("./curl.sh");
}
}
原问题
我的 .then()
块在 createInsertCurlReq.createInsertCurlReq(process.argv[2])
之后没有 运行。我应该如何重写它才能做到这一点?我注意到如果我不删除文件而只是附加到脚本 运行s.
async function testRun() {
if (fs.existsSync('./curl.sh')) {
shell.rm('-rf', './curl.sh');
}
createInsertCurlReq.createInsertCurlReq(process.argv[2]).then(() => {
shell.chmod('+x', './curl.sh');
shell.exec('./curl.sh')
return
})
}
}
curlCreation.js
function createInsertCurlReq(filePath) {
return utils
.readJSONFileOnDisk(filePath)
.then(data => {
obj = JSON.parse(data);
let resultArr = [];
for (let key in obj) {
for (let innerKey in obj[key]) {
let arrayNew = {};
arrayNew[key] = obj[key][innerKey].resolved;
resultArr.push(arrayNew);
}
}
return resultArr;
})
.then(data => {
if (!fs.existsSync("./curl.sh")) {
shell.touch("./curl.sh");
}
return data;
})
.then(data => {
for (let i = 0; i < data.length; i++) {
for (let key in data[i]) {
fs.appendFile(
"curl.sh",
`curl -X POST -H "xxx",
function(err) {
if (err) throw err;
}
);
}
}
});
}
module.exports = { createInsertCurlReq: createInsertCurlReq };
我认为问题是您在 createInsertCurlReq
的最后 then
调用中有一个同步循环。不同但相关的问题:
尝试用类似的东西替换它:
const {promisify} = require('util');
const appendFileAsync = promisify(fs.appendFile);
// ...
.then(data => Promise.all(data.map(() => appendFileAsync(
"curl.sh",
`curl -X POST -H "xxx"`
).catch(err => { throw err; }))));
这样所有 fs.appendFile
呼叫将在进入下一个 then
之前解决。
如果您不介意引入模块,您还可以使用 async-af
重构为异步 forEach 方法:
const {promisify} = require('util');
const AsyncAF = require('async-af');
const appendFileAsync = promisify(fs.appendFile);
// ...
.then(data => AsyncAF(data).forEach(() => appendFileAsync(
"curl.sh",
`curl -X POST -H "xxx"`
).catch(err => { throw err; })));
在 createInsertCurlReq 中,当没有异步操作时,您使用然后链接 jsut 来传递数组。试试这个格式:
async function testRun() {
if (fs.existsSync('./curl.sh')) {
shell.rm('-rf', './curl.sh');
}
await createInsertCurlReq.createInsertCurlReq(process.argv[2])
shell.chmod('+x', './curl.sh');
shell.exec('./curl.sh');
}
async function createInsertCurlReq(filePath) {
const data = await utils.readJSONFileOnDisk(filePath);
const obj = JSON.parse(data);
let resultArr = [];
for (let key in obj) {
for (let innerKey in obj[key]) {
let arrayNew = {};
arrayNew[key] = obj[key][innerKey].resolved;
resultArr.push(arrayNew);
}
}
if (!fs.existsSync("./curl.sh")) {
shell.touch("./curl.sh");
}
for (let i = 0; i < resultArr.length; i++) {
for (let key in resultArr[i]) {
fs.appendFileSync(
"curl.sh",
`curl -X POST -H "xxx"`
);
}
}
}
更新:我在写入文件后添加了一个短暂的暂停,直到那时 shell 命令才起作用。
async function testRun() {
await createInsertCurlReq.createInsertCurlReq(process.argv[2]);
await timeout(3000);
shell.chmod("+x", "./curl.sh");
shell.exec("./curl.sh");
}
}
原问题
我的 .then()
块在 createInsertCurlReq.createInsertCurlReq(process.argv[2])
之后没有 运行。我应该如何重写它才能做到这一点?我注意到如果我不删除文件而只是附加到脚本 运行s.
async function testRun() {
if (fs.existsSync('./curl.sh')) {
shell.rm('-rf', './curl.sh');
}
createInsertCurlReq.createInsertCurlReq(process.argv[2]).then(() => {
shell.chmod('+x', './curl.sh');
shell.exec('./curl.sh')
return
})
}
}
curlCreation.js
function createInsertCurlReq(filePath) {
return utils
.readJSONFileOnDisk(filePath)
.then(data => {
obj = JSON.parse(data);
let resultArr = [];
for (let key in obj) {
for (let innerKey in obj[key]) {
let arrayNew = {};
arrayNew[key] = obj[key][innerKey].resolved;
resultArr.push(arrayNew);
}
}
return resultArr;
})
.then(data => {
if (!fs.existsSync("./curl.sh")) {
shell.touch("./curl.sh");
}
return data;
})
.then(data => {
for (let i = 0; i < data.length; i++) {
for (let key in data[i]) {
fs.appendFile(
"curl.sh",
`curl -X POST -H "xxx",
function(err) {
if (err) throw err;
}
);
}
}
});
}
module.exports = { createInsertCurlReq: createInsertCurlReq };
我认为问题是您在 createInsertCurlReq
的最后 then
调用中有一个同步循环。不同但相关的问题:
尝试用类似的东西替换它:
const {promisify} = require('util');
const appendFileAsync = promisify(fs.appendFile);
// ...
.then(data => Promise.all(data.map(() => appendFileAsync(
"curl.sh",
`curl -X POST -H "xxx"`
).catch(err => { throw err; }))));
这样所有 fs.appendFile
呼叫将在进入下一个 then
之前解决。
如果您不介意引入模块,您还可以使用 async-af
重构为异步 forEach 方法:
const {promisify} = require('util');
const AsyncAF = require('async-af');
const appendFileAsync = promisify(fs.appendFile);
// ...
.then(data => AsyncAF(data).forEach(() => appendFileAsync(
"curl.sh",
`curl -X POST -H "xxx"`
).catch(err => { throw err; })));
在 createInsertCurlReq 中,当没有异步操作时,您使用然后链接 jsut 来传递数组。试试这个格式:
async function testRun() {
if (fs.existsSync('./curl.sh')) {
shell.rm('-rf', './curl.sh');
}
await createInsertCurlReq.createInsertCurlReq(process.argv[2])
shell.chmod('+x', './curl.sh');
shell.exec('./curl.sh');
}
async function createInsertCurlReq(filePath) {
const data = await utils.readJSONFileOnDisk(filePath);
const obj = JSON.parse(data);
let resultArr = [];
for (let key in obj) {
for (let innerKey in obj[key]) {
let arrayNew = {};
arrayNew[key] = obj[key][innerKey].resolved;
resultArr.push(arrayNew);
}
}
if (!fs.existsSync("./curl.sh")) {
shell.touch("./curl.sh");
}
for (let i = 0; i < resultArr.length; i++) {
for (let key in resultArr[i]) {
fs.appendFileSync(
"curl.sh",
`curl -X POST -H "xxx"`
);
}
}
}