We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
class QueueJob { queue(callback) { const previous = this.pending || Promise.resolve() this.pending = previous.then(callback).catch(callback) // 利用闭包保存当前pending const current = this.pending current.then(() => { // this.pending 完成时,this.pending没有被覆盖 if(this.pending === current) { delete this.pending } }) } } var queueJob = new QueueJob() queueJob.queue(() => { return new Promise((resolve, reject) => { setTimeout(() => { console.log('first') resolve() }, 2000) }) }) queueJob.queue(() => { return new Promise((resolve, reject) => { setTimeout(() => { console.log('second') resolve() }, 1000) }) }) queueJob.queue(() => { return new Promise((resolve, reject) => { setTimeout(() => { console.log('three') resolve() }, 500) }) }) // first // second // three
The text was updated successfully, but these errors were encountered:
No branches or pull requests
功能描述
扩展
The text was updated successfully, but these errors were encountered: