How to lock (Mutex) in NodeJS?

孤街浪徒 提交于 2019-12-02 11:31:31

I'd use something like the async module's queue and set its concurrency parameter to 1. That way, you can put as many tasks in the queue as you need to run, but they'll only run one at a time.

The queue would look something like:

var inventoryQueue = async.queue(function(task, callback) {
    // use the values in "task" to call your inventory API here
    // pass your results to "callback" when you're done
}, 1);

Then, to make an inventory API request, you'd do something like:

var inventoryRequestData = { /* data you need to make your request; product id, etc. */ };
inventoryQueue.push(inventoryRequestData, function(err, results) {
    // this will be called with your results
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!