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!