javascript - Mocha - 运行和解析两个异步/等待调用

标签 javascript asynchronous ecmascript-6 mocha.js

我很难让它正常工作。我无法让第二个异步调用运行:

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 中看到的: enter image description here 注意:图片显示我有 (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/

相关文章:

javascript - Node.js 应用程序中的阻塞功能

javascript - 垃圾收集未使用的模块

asynchronous - 为什么 Rc 在 async fn 中工作

javascript - 异步函数返回未定义且 $q 延迟

javascript - 在 Extjs 中加载视口(viewport)之前,非 MVC 创建的商店不会实例化

javascript - 从 $http.get() 返回的数据不是序列

javascript - 如何在 for 循环中访问 i 的当前数量?

Javascript "for of"在 IE 11 中失败

javascript - JS : this reference does not work

javascript - Webpack:将动态导入的模块拆分为单独的 block ,同时将库保留在主包中