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.
I want to know if there is any way at all to use the data from a resolved promise in 'normal' synchronous code
There isn't a way to write completely synchronous code when handling asynchronous responses. Once any operation is asynchronous, you have to deal with the response using asynchronous techniques and cannot program with it synchronously. You must learn to program asynchronously.
The two options you show (.then()
or async/await
) are your two choices for handling the returned promise.
or if you need to always 'wrap' all your logic that uses the data from a resolved promise in an async function.
If you want to use await
so that you can write synchronous-looking code for dealing with promises, then all that code has to be inside an async
function. And, as soon as you leave the scope of that function (like want to return a value), you're returning a promise from the async
function and again have to deal with the promise.
There is no way around this. It's just something one must learn in Javascript. It becomes a second nature after awhile.
As you appear to know, you can use async
and await
to get some synchronous-looking logic flow, but there are some things to make sure you understand when doing this. From your example:
async function test() {
const result = await Library.functionReturningAPromise()
const obj = new Example(result);
// do the rest of the logic
}
async
return a promise. That's the only kind of return value you get from them. If the caller is looking for a return value or wants to know when the asynchronous operations are done or is looking for errors, they MUST use that returned promise with .then()
, .catch()
or again with await
inside an async
function.await
and you need to handle rejections locally, then you need to wrap them in try/catch
(very similar syntax to synchronous exceptions).Here's an example of using try/catch
with await
to handle errors locally:
async function test() {
try {
const result = await Library.functionReturningAPromise();
const obj = new Example(result);
// do the rest of the logic
} catch(e) {
// handle promise rejection here
}
}
Here's an example of handling errors by the caller:
async function test() {
const result = await Library.functionReturningAPromise();
const obj = new Example(result);
// do the rest of the logic
}
test().then(() => {
console.log("all done");
}).catch(err => {
console.log(err);
});