This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getUser = async (query) => { | |
const user = await Users.findOne(query); | |
const feed = await Feeds.findOne({ user: user._id }); | |
return { user, feed }; | |
}; | |
// getUser will return a promise | |
getUser({ username: 'test' }).then(...); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const { promisify } = require('util'); | |
// Function#bind as needed | |
const readFile = promisify(fs.readFile); | |
const writeFile = promisify(fs.writeFile); | |
const transform = () => /* ... */; | |
const transformFileAsync = async (source, dest) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const throwsLater = async () => { | |
await PromiseThatThrows('Some error'); | |
}; | |
async () => { | |
try { | |
// Note that without await | |
// the error will never be | |
// caught | |
await throwsLater; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PromiseThatThrows = (message) => new Promise((resolve, reject) => { | |
// Reject after 100ms with a new error | |
setTimeout(() => reject(new Error(message))), 100); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async (items) => { | |
for (let i = 0; i < items.length; i++) { | |
const result = await db.get(items[i]); | |
console.log(result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async (items) => { | |
let promises = []; | |
for (let i = 0; i < items.length; i++) { | |
promises.push(db.get(items[i])); | |
} | |
const results = await Promise.all(promises); | |
console.log(results); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async (items) => { | |
// Note that async functions return a promise | |
const promises = items.map(async (item) => { | |
const result = await db.get(item); | |
// Log individual results as they finish | |
console.log(result); | |
return result; | |
}); | |
const results = await Promise.all(promises); | |
console.log(results); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [user1, user2] = await Promise.all([db.get('user1'), db.get('user2')]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const immediatePromise = () => new Promise((resolve) => setImmediate(resolve)); | |
const timeoutPromise = (timeout) => new Promise((resolve) => setTimeout(resolve, timeout)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const z = async () => { | |
await x(); | |
await timeoutPromise(1000); // Wait 1 second | |
// You can recurse z if needed | |
return y(); | |
} |
OlderNewer