javascript - 循环中的 Javascript Promises 在玩笑测试期间表现不同
问题描述
我有这个具有数组映射和三层嵌套承诺的函数。索引.js
const promiseA=require('./promiseA')
console.log(' Starting ')
//setInterval(()=>console.log('executed'), 5000)
const sample=async ()=>{
const newArray=[1,2,3,4,5]
let result =undefined
await Promise.all(newArray.map(async()=>{
result = await promiseA();
console.log('after calling A')
}))
console.log(`final result ${result}`)
return result;
}
sample();
module.exports=sample
承诺A
const promiseB=require('./promiseB')
const promiseA=async()=>{
return Promise.resolve(await promiseB())
}
module.exports=promiseA
承诺乙
const promiseC=require('./promiseC')
const promiseB=async()=>{
return Promise.resolve(await promiseC())
}
module.exports=promiseB
承诺 C
const promiseC=async()=>{
return Promise.resolve([1,2,3])
}
module.exports=promiseC
当我执行 npm start 时,我得到以下输出
Starting
after calling A
after calling A
after calling A
after calling A
after calling A
final result 1,2,3
输出与预期的相同。
来开玩笑的。
index.test.js
const sample=require('../src/index')
describe('all tests', ()=>{
it('should', async()=>{
const result = await sample()
expect(result).toBeDefined();
})
})
当我进行 npm 测试时,我得到以下输出
console.log
Starting
at Object.<anonymous> (src/index.js:7:9)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
final result 1,2,3
at sample (src/index.js:33:13)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
after calling A
at Promise.all.newArray.map (src/index.js:19:17)
console.log
final result 1,2,3
at sample (src/index.js:33:13)
这是我不明白的事情。输出被打印两次。有人可以帮忙吗。在过去的两天里,我一直陷入类似的问题。
解决方案
index.js
const promiseA=require('./promiseA')
console.log(' Starting ')
//setInterval(()=>console.log('executed'), 5000)
const sample=async ()=>{
const newArray=[1,2,3,4,5]
let result =undefined
await Promise.all(newArray.map(async()=>{
result = await promiseA();
console.log('after calling A')
}))
console.log(`final result ${result}`)
return result;
}
sample(); // <- you are calling it here
module.exports=sample
index.test.js
const sample=require('../src/index')
describe('all tests', ()=>{
it('should', async()=>{
const result = await sample() // <- you are calling it here
expect(result).toBeDefined();
})
})
你打sample
了两次电话,一次在index.js
,一次在index.test.js
推荐阅读
- mysql - 主键的 PhpMyAdmin 重复条目
- javascript - 将 2 个字母的国家代码转换为国家名称
- bash - 使用哪些工具列出所有 lineageOS ROM 的定价?
- google-drive-api - Google Drive API,更改 api - 共享文件缺少父字段
- reactjs - Recharts 雷达图上的自定义标签
- c# - 是否可以获得授权的返回或结果?
- php - Woocommerce get_products 返回空对象
- python - 在 Windows 10 中设置 Python 路径以安装第三方 Python 包
- arrays - 对工作表的字符串引用有效,但字符串变量无效
- r - 面板数据与 R