我正在努力工作。我无法进行第二次异步调用:
it.only('can delete an S3 bucket for a given PR', async() => {
let result
const options = { branch: '11'}
// this runs and completes just fine (as you can see in the webstorm pic)
await Deploy.createPRBucket(options, (error, stdout, stderr) => {
console.log(`created error: ${error}`)
console.log(`created stdout: ${stdout}`)
console.log(`created stderr: ${stderr}`)
result = JSON.parse(stdout)
console.log("added")
})
// it never hits my console log and bombs before that right after the createPRBucket call resolves
console.log("deleting...")
await Deploy.deletePRBucket(options.branch, (error, stdout, stderr) => {
console.log(`deleted error: ${error}`)
console.log(`deleted stdout: ${stdout}`)
console.log(`deleted stderr: ${stderr}`)
expect(stdout).to.exist
})
})
守则:
export async function createPRBucket (options, callback){
try {
await exec(`aws s3api create-bucket --bucket ${options.branch} --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2`,
(error, stdout, stderr) => {
console.error(`exec error: ${error}`);
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
callback(error, stdout, stderr)
// process.exit(0)
})
let error, stdout, stderr
}
catch(err) {
console.log(`there was a problem creating this bucket: ${err}`)
// process.exit(1)
}
}
export async function deletePRBucket(branch, callback){
await exec(`aws s3 rb s3://xxx-${branch} --force`,
(error, stdout, stderr) => {
console.error(`exec error: ${error}`);
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
callback(error, stdout, stderr)
// process.exit(0)
})
}
这是我在WebStorm中看到的内容:
注意:图片显示我有(done) =>
和done()
,但由于我使用async () =>
,我们已将其删除。无论哪种方式,我都会遇到同样的问题,同样的错误。
同样注意我可以在我的测试中在第一个await调用(createPRBranch)的回调中放置一个断点,它就可以了。但是如果我在第二次调用中设置断点(等待deletePRBranch),则第二次调用永远不会被触发,也不会解析该调用。
答案 0 :(得分:2)
鉴于exec是一个util.promisifed child_process.exec
it.only('can delete an S3 bucket for a given PR', async() => {
let result;
const options = { branch: '11' };
// this runs and completes just fine (as you can see in the webstorm pic)
let {stdout, stderr} = await Deploy.createPRBucket(options);
console.log(`created error: ${error}`);
console.log(`created stdout: ${stdout}`);
console.log(`created stderr: ${stderr}`);
result = JSON.parse(stdout);
console.log("added");
console.log("deleting...");
({stdout, stderr} = await Deploy.deletePRBucket(options.branch));
console.log(`deleted error: ${error}`);
console.log(`deleted stdout: ${stdout}`);
console.log(`deleted stderr: ${stderr}`);
expect(stdout).to.exist
})
导出的函数可以简单地 - 注意,不需要async
因为它们已经返回Promise
export function createPRBucket(options) {
return exec(`aws s3api create-bucket --bucket ${options.branch} --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2`);
}
export function deletePRBucket(branch) {
return exec(`aws s3 rb s3://xxx-${branch} --force`);
}
promisified exec返回的承诺解决了
{ stdout: "...", stderr: "..." }
注意:我还没有针对
exec
的错误情况进行测试 - 错误对象是
{
killed: true or false,
code: a number?,
signal: null (not sure what else can be here,
cmd: "the original command line that was run"
}
的解释
({stdout, stderr} = await Deploy.deletePRBucket(options.branch));
当使用没有声明的对象文字解构赋值时,赋值语句周围的(..)是必需的语法。
{a,b} = {a:1,b:2}不是有效的独立语法,因为左侧的{a,b}被视为块而不是对象文字。< / p>
然而,({a,b} = {a:1,b:2})是有效的,因为var {a,b} = {a:1,b:2}
注意:你的(..)表达式需要以分号开头,或者它可以用来执行前一行的函数。
来源:MDN Docs