Async/Await combined with Promises
await works with any promise, not only with built-in ones such as fetch(). So a function that returns a promise can be called with await inside an async function, and the promise methods such as catch() and Promise.all() still work alongside it. This page uses one small promise-returning function throughout: it checks an item’s stock after a delay, and it rejects for an item that has run out.
const checkStock = (item, ms) => new Promise((resolve, reject) => { setTimeout(() => { if (item === 'saffron') { reject(new Error(`${item} is out of stock`)); } else { resolve(`${item} in stock`); } }, ms); });Awaiting your own promise
Section titled “Awaiting your own promise”await gives back the value that the promise resolves with. When the promise rejects, await throws the rejection reason, so try...catch handles both outcomes:
async function order(item) { try { const message = await checkStock(item, 50); console.log(message); } catch (err) { console.log(`Failed: ${err.message}`); }}
order('rice').then(() => order('saffron'));This prints:
rice in stockFailed: saffron is out of stockMixing await with catch()
Section titled “Mixing await with catch()”await applies to the whole expression after it, so you can attach catch() to the promise and await the result. The catch() callback turns a rejection into an ordinary value, which suits cases where a failure has a sensible fallback:
async function order(item) { const message = await checkStock(item, 50).catch((err) => `Failed: ${err.message}`); console.log(message);}
order('saffron');This prints:
Failed: saffron is out of stockBe careful with this pattern. A catch() that returns nothing makes the awaited value undefined, and the code after it then carries on as though the call succeeded. Return a value that the code can recognise, or rethrow.
Running tasks together
Section titled “Running tasks together”Each await waits for its promise before the next line starts, so two independent checks in a row take twice as long as one. Start both, and then wait for both with Promise.all():
async function oneByOne() { const start = Date.now(); await checkStock('rice', 200); await checkStock('lentils', 200); console.log(`one by one: about ${Math.round((Date.now() - start) / 100) * 100} ms`);}
async function together() { const start = Date.now(); const results = await Promise.all([checkStock('rice', 200), checkStock('lentils', 200)]); console.log(results); console.log(`together: about ${Math.round((Date.now() - start) / 100) * 100} ms`);}
oneByOne().then(together);This prints:
one by one: about 400 ms[ 'rice in stock', 'lentils in stock' ]together: about 200 msPromise.all() keeps the results in the order of the input array, whatever order the tasks finish in. It rejects as soon as any one promise rejects.
Keeping every result
Section titled “Keeping every result”When some tasks may fail and you still want the others, Promise.allSettled() waits for all of them and reports each outcome as an object with a status of 'fulfilled' or 'rejected':
async function checkAll(items) { const results = await Promise.allSettled(items.map((item) => checkStock(item, 50))); for (const r of results) { console.log(r.status === 'fulfilled' ? r.value : r.reason.message); }}
checkAll(['rice', 'saffron', 'lentils']);This prints:
rice in stocksaffron is out of stocklentils in stockThe JavaScript Promises series covers Promise.all(), Promise.allSettled(), Promise.race() and Promise.any() in more depth.
Steve Griffith covers this topic in his video Combining Async Await with Promises.