首页 > 解决方案 > 循环中的 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)

这是我不明白的事情。输出被打印两次。有人可以帮忙吗。在过去的两天里,我一直陷入类似的问题。

标签: javascriptpromise

解决方案


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


推荐阅读