我很难让它正常工作。我无法让第二个异步调用运行:
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()
但因为我正在使用 所以把它们去掉了异步 () =>
。无论哪种方式,我都会遇到同样的问题,同样的错误。
另请注意,我可以在测试中的第一个 await 调用 (createPRBranch) 的回调中放置一个断点,它会正常运行。但是,如果我在第二次调用中放置一个断点(等待 deletePRBranch),第二次调用永远不会被命中,调用也不会解析。
最佳答案
鉴于 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`);
}
promised exec 返回的 promise 解析为
{ stdout: "...", stderr: "..." }
Note: I haven't tested for error condition of
exec
- the error object is
{
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));
The ( .. ) around the assignment statement is required syntax when using object literal destructuring assignment without a declaration.
{a, b} = {a: 1, b: 2} is not valid stand-alone syntax, as the {a, b} on the left-hand side is considered a block and not an object literal.
However, ({a, b} = {a: 1, b: 2}) is valid, as is var {a, b} = {a: 1, b: 2}
NOTE: Your ( ..) expression needs to be preceded by a semicolon or it may be used to execute a function on the previous line.
source: MDN Docs
关于javascript - Mocha - 运行和解析两个异步/等待调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449964/