Search code examples
node.jsnpmnodesnpm-scripts

NPM run parallel task, but wait until resource is available to run second task


In npm, how can I run two or more parallel tasks, but waiting for the resource that the first task will create to be available to the second task to use it, and so forth?

example (conceptual):

npm run task1 & waitfor task1 then task2 & waitFor task3 then task4 ...

any ideas?

EDIT

As an example: Lets say that my first task is starting a webserver, and my second task is consuming data from that web-server every time an event happens. Another example: My first task could be starting webdriver-manager, my second task, starting a webserver, and my third task, run e2e tests everty time my files are changed. So, I need all those tasks to keep running concurrently, but they need to be initialized in an specific order and time.


Solution

  • You can try concurrently with wait-on package in order to manage conccurent/sequential scripts and outputs. wait-on sequential launches support head response status, tcp listening, ...

    For example :

    "scripts": {
        "start": "concurrently -k -n \"DB,API,WEB\" -c \"blue,magenta,yellow\" \"npm run start-db\" \"npm run start-api\" \"npm run start-web\"",
        "start-db": "myDbServerCmd",
        "start-api": "wait-on tcp:27017 && myApiServerCmd",
        "start-web": "myFrontServerCmd",
    }
    

    Thanks dchambers for the idea (source).