OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Calling .then within a new Promise() javascript

  • Thread starter Thread starter JackG
  • Start date Start date
J

JackG

Guest
I've learned javascript async/await before learning promise.then() syntax, and I am now attempting to go back and learn promise.then().

I currently have the following code

Code:
let getDatabaseData = async () => {
    let res1 = await dbQuery()
    let res2 = await dbQuery2(res1)
    return res1 + res2
}

let dbData = await getDatabaseData()
console.log(dbData)

I want to translate this to promises, and have done so below

Code:
let getDatabaseData = () => {
    return new Promise(resolve => {
        dbQuery().then(res1 => {
            dbQuery2(res1).then(res2 => {
                resolve(res1 + res2)
            })
        })
    })
}

getDatabaseData().then(dbData => {
    console.log(dbData)
})

Is this the most effective way to do so? I was looking into promise chaining, but it doesn't exactly get me what I want because I want getDatabaseData to act as a helper method to combine the results from dbQuery and dbQuery2

<p>I've learned javascript async/await before learning promise.then() syntax, and I am now attempting to go back and learn promise.then().</p>
<p>I currently have the following code</p>
<pre><code>let getDatabaseData = async () => {
let res1 = await dbQuery()
let res2 = await dbQuery2(res1)
return res1 + res2
}

let dbData = await getDatabaseData()
console.log(dbData)
</code></pre>
<p>I want to translate this to promises, and have done so below</p>
<pre><code>let getDatabaseData = () => {
return new Promise(resolve => {
dbQuery().then(res1 => {
dbQuery2(res1).then(res2 => {
resolve(res1 + res2)
})
})
})
}

getDatabaseData().then(dbData => {
console.log(dbData)
})
</code></pre>
<p>Is this the most effective way to do so? I was looking into promise chaining, but it doesn't exactly get me what I want because I want getDatabaseData to act as a helper method to combine the results from dbQuery and dbQuery2</p>
 

Latest posts

Top