Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const work = (() => {
- let workload = []
- let cursor = null
- setImmediate(async () => {
- while (true) {
- const workload = await next()
- // implement as much as possible outside the loop
- for (const { context, args, resolve, reject } of workload) {
- reject(new Error('not implemented'))
- }
- }
- })
- const next = () => new Promise(resolve => {
- cursor = () => {
- if (workload.length === 0) return
- cursor = null
- const nextWorkload = [...workload]
- workload = []
- resolve(nextWorkload)
- }
- cursor()
- })
- return function work (...args) {
- const context = this
- return new Promise((resolve, reject) => {
- workload.push({ context, args, resolve, reject })
- if (typeof cursor === 'function') cursor()
- })
- }
- })()
- work().then(console.log).catch(console.error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement