queue-work
v0.0.2
Published
queue-work
Downloads
2
Readme
queue-work
Installation
npm install queue-work
Usage
import { QueueWork } from 'queue-work'
function mywork(val) {
return new Promise(resolve => {
setTimeout(() => {
console.log(`val: ${val}`)
resolve({
value: val
})
}, val)
})
}
const queueWork = new QueueWork()
queueWork.add({
work: () => mywork(500)
})
queueWork.resolve()
Example1
import { QueueWork } from 'queue-work'
const queueWork = new QueueWork()
queueWork.add({
work: () => mywork(300),
wait: 2000
})
queueWork.add({
work: () => mywork(600),
handler: (next, res) => {
if (res.value === 600) {
next() //
}
}
})
queueWork.add({
work: () => mywork(300),
})
const res = await queueWork.resolve()
console.log(res)
Example2
import queue from 'queue-work'
const res = await queue.qw([
{
work: () => mywork(300),
wait: 2000
},
{
work: () => mywork(600),
handler: (next, res) => {
if (res.value === 600) {
next() //
}
}
},
{
work: () => mywork(300),
}
])
console.log(res)
Example3
import queue, { QueueWork } from 'queue-work'
queue.QUEUE_WORK_CONFIG.beforeWork = (works) => {
//loading.show
console.log('beforeWork')
}
queue.QUEUE_WORK_CONFIG.afterWork = (works, responses) => {
//loading.hide
console.log('afterWork')
}
queue.QUEUE_WORK_CONFIG.beforeQueueWork = (works, curWork) => {
console.log('beforeQueueWork')
}
queue.QUEUE_WORK_CONFIG.afterQueueWork = (works, curWork, res) => {
console.log('afterQueueWork')
}
const queueWork = new QueueWork()
queueWork.add({
work: () => mywork(500),
handler() {
console.log('finish')
}
})
queueWork.resolve()
/*
beforeWork
beforeQueueWork
500
afterQueueWork
finish
afterWork
*/