So I have a couple of async/await functions and I'd like to queue them. They're fired "randomly" on specific websocket events but they have to run one after the other. So I'm testing this with this code.
const aida = {
async launch() {
await someFunction();
},
async login() {
await Auth();
await UserSettings();
},
async goToMonth() {
await JumpToDefaultMonth();
}
};
const q = async.queue(function(task, callback) {
console.log('hello ' + task.name);
callback();
}, 1);
q.drain = function() {
console.log('all items have been processed');
};
q.push(aida.launch());
q.push(aida.login());
q.push(aida.goToMonth());
I'd expect that for example login()
only runs after launch()
in finished.
Am I doing something wrong or is this library not the right one for me?
Hi @codeofsumit, thanks for the question!
From looking at your code, I think the issue is with the last three lines. You're invoking the functions, and pushing their return value into the queue
as opposed to pushing the functions into the queue
. See the QueueObject
docs for more info about q.push
. Try changing them to:
q.push(aida.launch);
q.push(aida.login);
q.push(aida.goToMonth);
Since you have a concurrency
of 1
, the queue will only process login
after launch
finished.
@hargasinski I thought so and tried that but that brings two new questions:
@codeofsumit sorry, I realized my previous comment was a little misguide, I got queue
confused with a different function. Hopefully I can clarify.
- How does the queue start? Didn't seem to start after the first push
queue
should start when it gets a task
, i.e. when you call q.push
. Could you provide a code sample? The above code ran for me.
Side note: If you are using es7 async
functions with async
and transpiling your code, you will have to wrap them in async.asyncify
. See this comment for more info.
- How can I pass parameters to the functions?
This is where I got confused. The worker
you pass to async.queue
when you create it, is responsible for processing the task. If you need to run launch
, login
, and goToMonth
for each event from the websocket, you can just put in the worker
function. For example,
const q = async.queue(function(task, callback) {
async.series([
async function() {
await aida.launch(task.foo, task.boo);
},
async function() {
await aida.login(task.baz);
},
// etc...
], callback);
}, 1);
// on a websocket event
q.push({foo: websocketData.foo, bar: websocketData.bar, baz: websocketData.baz});
Otherwise, if you need to queue functions, it's a little hacky, but you could something along the lines of:
const q = async.queue(async function(task) {
await task.func.apply(null, task.args);
}, 1);
// on a websocket event
q.push({func: aida.launch, args: [websocketData.foo, websocketData.bar]});
q.push({func: aida.login, args: [websocketData.baz]});
q.push({func: aida.goToMonth, args: []});
Hopefully this is a little clearer.
uhh this looks very promising. Thank you for taking the time to explain it in such detail. Helps a lot!
Also: I noticed you're resolving issues in this repo VERY fast (not much open issues for such a popular library). 馃憤 very impressive - keep up the good work!
Most helpful comment
@codeofsumit sorry, I realized my previous comment was a little misguide, I got
queue
confused with a different function. Hopefully I can clarify.queue
should start when it gets atask
, i.e. when you callq.push
. Could you provide a code sample? The above code ran for me.Side note: If you are using es7
async
functions withasync
and transpiling your code, you will have to wrap them inasync.asyncify
. See this comment for more info.This is where I got confused. The
worker
you pass toasync.queue
when you create it, is responsible for processing the task. If you need to runlaunch
,login
, andgoToMonth
for each event from the websocket, you can just put in theworker
function. For example,Otherwise, if you need to queue functions, it's a little hacky, but you could something along the lines of:
Hopefully this is a little clearer.