javascript - async.queue with asyncawait style functions - Stack Overflow

I'm trying to create a function that builds a queue from an array of objects and then processes ea

I'm trying to create a function that builds a queue from an array of objects and then processes each object by calling a number of functions.

The processing functions are asynchronous functions which, prior to needing to queue, I'd implemented using the async/await pattern. I think this is necessary as each relies on the output of the previous and I don't want to have a tonne of nested promise.then's

i.e. previously I had:

await Promise.all(messages.map(async(message) => {
    let activity = await activityController.getActivity(message.activityId);
    let url = await SMSController.getUrl(message.Token);
    let smsSendResult = await SMSController.sendSMS(messageString, activity.mobileNo);
    // etc...
}

Now what I want to be able to do is:

let queue = async.queue((message, done) => {
     let activity = await activityController.getActivity(message.activityId);
     let smsSendResult = await SMSController.sendSMS(messageString, activity.mobileNo);
    // etc...
}

messages.forEach((message) => {
    queue.push(message);
})

I have the problem though that this results in

SyntaxError: await is only valid in async function

And I can't seem to quite get my head around how to get past this.

I'm trying to create a function that builds a queue from an array of objects and then processes each object by calling a number of functions.

The processing functions are asynchronous functions which, prior to needing to queue, I'd implemented using the async/await pattern. I think this is necessary as each relies on the output of the previous and I don't want to have a tonne of nested promise.then's

i.e. previously I had:

await Promise.all(messages.map(async(message) => {
    let activity = await activityController.getActivity(message.activityId);
    let url = await SMSController.getUrl(message.Token);
    let smsSendResult = await SMSController.sendSMS(messageString, activity.mobileNo);
    // etc...
}

Now what I want to be able to do is:

let queue = async.queue((message, done) => {
     let activity = await activityController.getActivity(message.activityId);
     let smsSendResult = await SMSController.sendSMS(messageString, activity.mobileNo);
    // etc...
}

messages.forEach((message) => {
    queue.push(message);
})

I have the problem though that this results in

SyntaxError: await is only valid in async function

And I can't seem to quite get my head around how to get past this.

Share Improve this question edited Feb 9, 2018 at 11:48 Ross Coundon asked Feb 9, 2018 at 11:42 Ross CoundonRoss Coundon 9271 gold badge12 silver badges20 bronze badges 1
  • Try github./baryon/named-promise-task – Baryon Lee Commented Jul 29, 2020 at 1:49
Add a ment  | 

4 Answers 4

Reset to default 1

You're looking for async.series, not async.queue:

series(tasks, callbackopt)

Run the functions in the tasks collection in series, each one running once the previous function has pleted.

So just following the docs:

const messageCallbacks = messages.map(function(msg) {
    return async function(callback) {callback(await handleMessage(msg));
});


async.series(messageCallbacks,
// optional callback
function(err, results) {
    // results is now equal to whatever handleMessage resolves to
});

Without async:

async function asyncMessageQueue(messages) {
    const results = [];
    for(var i=0,l=messages.length; i<l; ++i) {
        results.push(await handleMessage(messages[i]));
    }
    return results;
}

async function handleMessage(message) {
        let activity = await activityController.getActivity(message.activityId);
        let url = await SMSController.getUrl(message.Token);
        let smsSendResult = await SMSController.sendSMS(messageString, activity.mobileNo);
        // rest of the code
};

This also allows you to provide the next message with any previous results:, just change await handleMessage(messages[i]) to await handleMessage(messages[i], results) and then:

async function handleMessage(message, prevResults) {
        // rest of the code
};

I found the asyncify function in the async module which allows me to do this:

var queue = async.queue(async.asyncify(async (message, done) => {
    let url = await SMSController.getUrl(message.token);
    // etc...
}

You just need to use the async keyword in the callback function:

var queue = async.queue(async (message, done) => {
    let url = await SMSController.getUrl(message.token);
    // etc...
})

so recently I fall in this problem too, where I have a queue and I have a await process to inside the queue...

Solution ;

var queue = async.queue( (message, done) => {
    return new Promise(async(resolve, reject) => {
      const response = await function_name(); //await the function
      done(null, response); //call the callback function..
      //or
      resolve(done(null, response));
    });
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744671776a4587086.html

相关推荐

  • javascript - async.queue with asyncawait style functions - Stack Overflow

    I'm trying to create a function that builds a queue from an array of objects and then processes ea

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信