Javascript: calling asynchronous code with map and testing it in Jest

const items = ["a","b"]
const performSth = (item) => {
console.log(item)
}
const endresult = items.map((item) => {
performSth(item)
if (item == "a") {
console.log("first")
return("{1,2,3}")
} else if (item == "b") {
console.log("second")
return(1+2)
}
})
console.log(`res ${endresult}`)

class TestMe {
constructor() {
}
performSth (item) {
console.log(item)
}
addData (items){
try {
items.map( (item) => {
this.performSth(item)
if (item == "a") {
console.log("first")
return "{1,2,3}";
} else if (item == "b") {
console.log("second")
return 1;
}
})
}
catch (err) {
console.warn(`Error ${err}`)
}
}
}
testme = new TestMe()
const items = ["a","b"]
let result = testme.addData(items)
console.log(`res ${result}`)
try {
const retmap = items.map( (item) => {
this.performSth(item)
if (item == "a") {
console.log("first")
return "{1,2,3}";
} else if (item == "b") {
console.log("second")
return 1;
}
})
return(retmap)
}
try {
const retmap = await Promise.all(items.map( async (item) => {
await this.performSth(item)
if (item == "a") {
console.log("first")
return "{1,2,3}";
} else if (item == "b") {
console.log("second")
return 1;
}
}))
return(retmap)
}
const testMe = new TestMe();
let result = await testMe.addData(data);
console.log(result.length);
expect(result.length).to.equal(10);
it('will add amos product', async (done) => {

})
done();
expect(result.length).to.equal(2);

--

--

--

Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Built to last

Idea of monorepo for mobile apps with GCP

Wealth.ng Launches Ola — A personal Wealth Advisor on WhatsApp

Build a GraphQL Server with Node.js and Express

1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence

8 steps to get started with Redux and React — A roadmap ⚛ 💡 🏁

Playing around with recursive conditional types

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Victor Yeo

Victor Yeo

Engineer

More from Medium

Manage JavaScript Environment Variables with dotenv

Parallel & Sequential handling Javascript Promise

Features of Different Javascript Framework : Node Js, BackBone JS, Ext Js