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 function makeRequest() { | |
const response = await fetch('foo'); | |
if (response.doestItNeedAnotherRequest) { | |
const secondResponse = await makeAnotherRequest(response); | |
console.log(secondResponse); | |
return secondResponse; | |
} | |
console.log(response); | |
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
let promise = Promise.resolve(); | |
const posts = [{}, {}, {}]; | |
posts.forEach(post => { | |
promise = promise.then(() => { | |
return db.insert(post); | |
}); | |
}); | |
promise.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
posts.forEach(post => { | |
promise = promise.then(db.insert(post)); | |
}); |
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 posts = [{}, {}, {}]; | |
async function insertPosts(posts) { | |
for (let post of posts) { | |
await db.insert(post); | |
} | |
} |
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 posts = [{}, {}, {}]; | |
async function insertPosts(posts) { | |
posts.forEach(post => { | |
await db.insert(post); | |
}); | |
} |
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 posts = [{}, {}, {}]; | |
async function insertPosts(posts) { | |
posts.forEach(async post => { | |
const postId = await db.insert(post); | |
console.log(postId); | |
}); | |
// Not finished here | |
} |
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 posts = [{}, {}, {}]; | |
function insertPostsConcurrently(posts) { | |
return Promise.all(posts.map(doc => { | |
return db.insert(post); | |
})).then(results => { | |
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 posts = [{}, {}, {}]; | |
async function insertPostsConcurrently(posts) { | |
const promises = posts.map(post => db.insert(post)); | |
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 function getConcurrently() { | |
let promises = []; | |
promises.push(getUsers()); | |
promises.push(getCategories()); | |
promises.push(getProducts()); | |
let [users, categories, products] = await Promise.all(promises); | |
} |
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 assert = require('assert'); | |
test('Testing async code', () => { | |
getAsyncResult() // A | |
.then(first => { | |
assert.strictEqual(first, 'foo'); // B | |
return getAsyncResult2(); | |
}) | |
.then(second => { | |
assert.strictEqual(second, 'bar'); // C |