Skip to content

Instantly share code, notes, and snippets.

@peter-leonov
Last active October 18, 2017 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter-leonov/1ca214f4ea9dc9422e1c58702ea2e3b4 to your computer and use it in GitHub Desktop.
Save peter-leonov/1ca214f4ea9dc9422e1c58702ea2e3b4 to your computer and use it in GitHub Desktop.
Serial queue solution using lambda calculus.
const empty =
run =>
run()
const enqueue =
(q, fn) =>
next =>
q(() => fn(next))
let q = empty
q = enqueue(q, function(done){
console.log('first');
done();
});
q = enqueue(q, function(done){
console.log('second');
done();
});
q = enqueue(q, function(done){
setTimeout(function(){
console.log('third')
done()
}, 1000)
});
q = enqueue(q, function(done){
console.log('final task');
done();
});
// runs the tasks
q(function(){
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment