I'm learning about promises and I absolutely want to make sure I am understanding their use before I continue. I am using a library for an online service which has functions which return a promise.
Nearly all examples I read either use the resolved data in chained then()
functions
const result = Library.functionReturningAPromise()
result.then(function(res) {
const obj = new Example(res)
return obj
}).then(function(ob) {
// do the rest of the logic within these then() functions
})
or use the resolved data within an async
function
async function test() {
const result = await Library.functionReturningAPromise()
const obj = new Example(result)
// do the rest of the logic
}
I want to know if there is any way at all to use the data from a resolved promise in 'normal' synchronous code
const result = Library.functionReturningAPromise()
// do something to resolve the promise
const obj = new Example(result)
or if you need to always 'wrap' all your logic that uses the data from a resolved promise in an async
function.