Karim Alibhai - karim@alibhai.co
npx karimsa
libuv thread #1
libuv thread #2
libuv thread #3
libuv thread #N
await fs.readFile('blah.txt')
export async function sendMessage({ conversationID, messageID, message }) {
const users = await UserConversations.find({ conversationID })
const timeStart = Date.now()
for (const user of users) {
await sendPushNotification(user, { message })
await sendEmailNotification(user, { message })
await sendWebsocketUpdates(user, { type: 'new_message', message })
}
await markMessageAsDelivered({ messageID, conversationID })
console.log(`Sent message in ${Date.now() - timeStart}ms`)
}
Using async/await - Attempt #1
export async function sendMessage({ conversationID, messageID, message }) {
const users = await UserConversations.find({ conversationID })
const timeStart = Date.now()
await Promise.all(users, async user => {
await sendPushNotification(user, { message })
await sendEmailNotification(user, { message })
await sendWebsocketUpdates(user, { type: 'new_message', message })
})
await markMessageAsDelivered({ messageID, conversationID })
console.log(`Sent message in ${Date.now() - timeStart}ms`)
}
Using async/await - Attempt #2
export async function sendMessageToSingleUser({ userID, messageID }) {
if (!await pushNotifSent({ userID, messageID }) {
await sendPushNotification(user, { message })
await markPushNotificationSent({ userID, messageID })
}
if (!await emailSent({ userID, messageID }) {
await sendEmailNotification(user, { message })
await markEmailNotificationSent({ userID, messageID })
}
if (!await websocketUpdateSent({ userID, messageID }) {
await sendWebsocketUpdates(user, { type: 'new_message', message })
await markWebsocketSent({ userID, messageID })
}
}
export async function sendMessage({ conversationID, messageID }) {
const users = await UserConversations.find({ conversationID })
await saveTimeStarted(Date.now())
const jobIDs = await Promise.all(users, user => Queue.enqueue('send-message-to-user', {
userID: user._id,
message: messageID,
}))
await Queue.enqueue('mark-message-as-delivered', {
messageID,
}, {
dependencies: jobIDs,
})
}
export async function markMessageAsDelivered({ messageID }) {
await markMessageAsDelivered({ messageID, conversationID })
const timeStart = await getSavedTime()
await deleteSavedTime()
console.log(`Sent message in ${Date.now() - timeStart}ms`)
}
Thanks, Kent C. Dodds!
import { useState, forEach, waitFor, checkpoint } from '@karimsa/xs/core.macro'
export const sendMessage = routine(async ({ conversationID, messageID, message }) => {
const users = await UserConversations.find({ conversationID })
const timeStart = useState('timeStart', Date.now())
await forEach(users, async user => {
await waitFor(sendPushNotification(user, { message }))
checkpoint()
await waitFor(sendEmailNotification(user, { message }))
checkpoint()
await waitFor(sendWebsocketUpdates(user, { type: 'new_message', message }))
checkpoint()
})
await markMessageAsDelivered({ messageID, conversationID })
console.log(`Sent message in ${Date.now() - await timeStart.get()}ms`)
})
export const doStuff = routine(async N => {
const nums = useState([... new Array(N)])
await forEach(nums, i => {
console.log(i)
})
console.log('all done')
})
export const doStuff = routine(async N => {
const nums = [/* state magic */]
for (;; ++ctx.state) {
switch (ctx.state) {
case 0:
return this.produce(nums)
case 1:
console.log(ctx.yieldValue)
return this.halt()
case 3:
console.log('all done')
return this.done()
}
}
})
export const doStuff = routine(async N => {
const nums = [... new Array(N)]
const sum = useState('sum', 0)
await forEach(nums, i => {
return sum.incrby(i)
})
console.log(`The sum is: ${await sum.get()}`)
})
export const doStuff = routine(async N => {
const nums = [... new Array(N)]
const sum = await this.redis.set(`${this.execID}:sum`, 0, 'NX')
// forEach magics
await this.redis.incrby(`${this.execID}:sum`, i)
console.log(`The sum is: ${Number(await this.redis.get(`${this.execID}:sum`))}`)
}
Come talk to me after, I don't bite! (usually)
Karim Alibhai - karim@alibhai.co
https://alibhai.co