0

I was wondering if there is a way to get the returned value of the promise and assign it directly to a variable outside of a .then() call and use it?

Here is my implementation.

const name = "Jane"
const age = 34

// Promise 1
function getName() {
    return new Promise(function prom(resolve, reject) {
        setTimeout(function () {
            resolve(name) // "Jane"
        }, 5000)
    })
}

// Promise 2
function getAge() {
    return new Promise(function prom(resolve, reject) {
        setTimeout(function () {
            resolve(age) // 34
        }, 1500)
    })
}

// Promise all
function getValue(cb) {
    return Promise.all([
        getName() /* Jane */,
        getAge() /* 34 */
    ])
        .then(cb, cb)
}

After all the above i tried to do this:

const x = getValue(x => x)[1] // 34
const sum = x + 1; // 35

Any idea? Or is that possible?

Thanks!

Evangelos
  • 69
  • 9
  • 1
    [`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) provides syntactical sugar to sort of do that. – str Sep 03 '20 at 18:34
  • Await cannot be used outside an asynchronous function's execution context! It might work in Deno since await can be used at top level, but in vanilla JS I am not sure. – Evangelos Sep 03 '20 at 18:44
  • 1
    Is there a specific reason that you're wondering this, other than *"this feels odd and I'd like to get rid of it"*? – Tomalak Sep 03 '20 at 18:51
  • https://github.com/tc39/proposal-top-level-await – str Sep 03 '20 at 19:11
  • No, this is not possible. You're essentially asking "*Is there a way to get a value from the future immediately?*". – Bergi Sep 03 '20 at 19:26
  • @Bergi yes exactly is what I am asking. Deno provides a way for top level awaiting. Probably they have wrapped around the whole module into an async fn. I was wondering if that was possible with vanilla js. – Evangelos Sep 04 '20 at 06:14

1 Answers1

-1

You can use async/await to write this code in synchronous way and reduce the complexity of the code. Instead of using then() on promises, async/await provides a way to write asynchronous code in synchronous way and is also easy to understand.

Try following alternate to your code:

const name = "Jane"
const age = 34

// Promise 1
function getName() {
    return new Promise(function prom(resolve, reject) {
        setTimeout(function () {
            resolve(name) // "Jane"
        }, 5000)
    })
}

// Promise 2
function getAge() {
    return new Promise(function prom(resolve, reject) {
        setTimeout(function () {
            resolve(age) // 34
        }, 1500)
    })
}

// Promise all
async function getValue() { /*To use await, function should start with async keyword*/
    const userName = await getName();  /*Next line will not be executed until response is received here*/
    const userAge = await getAge();
    return Promise.all([
        userName /* Jane */,
        userAge /* 34 */
    ])
}

async function main(){
    const x =(await getValue())[1] // 34
    const sum = x + 1; // 35
    console.log(sum)
}

main()

Please refer to async/await topic to have better understanding of this concept.

Simran
  • 98
  • 6