Jos*_*hez 5 javascript async-await nightwatch.js
假设我有这个代码:
const myFunction = async => {
const result = await foobar()
}
const foobar = async () => {
const result = {}
result.foo = await foo()
result.bar = await bar()
return result
}
Run Code Online (Sandbox Code Playgroud)
我想要这个:
const myFunction = () => {
const result = foobar()
}
Run Code Online (Sandbox Code Playgroud)
我尝试像这样包装foobar :
const foobar = async () => {
return (async () => {
const result = {}
result.foo = await foo()
result.bar = await bar()
return result
})()
}
Run Code Online (Sandbox Code Playgroud)
但这仍然返回一个承诺
我不能在myFunction中使用.then,我需要foobar返回结果变量而不是承诺。
问题是myFunction是一个异步函数,它将返回一个承诺,但它应该返回 undefine 我需要摆脱myFunction中的 async 。
编辑:正如 Sebastian Speitel 所说,我想将myFunction转换为同步
编辑2:对Shilly来说,我正在使用nightwatch进行end2end测试,nightwatch将调用myFunction(),如果函数执行过程中没有错误,它将完美运行,如果出现错误,则nightwatch的虚拟机将永远运行而不是停止,如果被调用的函数是异步的,就会出现此问题。
您是否考虑过使用 .executeAsync() 然后让 Promise 调用 .done() 回调?这样就可以包装 foobar 并仅将异步或任何 .then() 调用保留在该包装器内。
我的守夜知识非常陈旧,但也许是这样的:
() => {
client.executeAsync(( data, done ) => {
const result = await foobar();
done( result );
});
};
Run Code Online (Sandbox Code Playgroud)
或者:
() => {
client.executeAsync(( data, done ) => foobar().then( result => done( result )));
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1748 次 |
| 最近记录: |