InterviewSolution
Saved Bookmarks
| 1. |
How can we use async await in node.js? |
|
Answer» Here is an EXAMPLE of using async-await PATTERN: // this code is to retry with exponential backofffunction wait (timeout) { return new PROMISE((resolve) => {setTimeout(() => { resolve()}, timeout); });}async function requestWithRetry (url) { const MAX_RETRIES = 10; for (let i = 0; i <= MAX_RETRIES; i++) {TRY { return await request(url);} catch (err) { const timeout = Math.pow(2, i); console.log('Waiting', timeout, 'ms'); await wait(timeout); console.log('Retrying', err.message, i);} }} |
|