Home > Mobile >  JavaScript Sequental operation
JavaScript Sequental operation

Time:03-21

What is the best way/pattern to perform sequental operations in JS(NodeJS)? E.g. Create folder->copy file->edit file->etc. This is stand-alone app, so thread-blocking oprations is ok(Sync).

CodePudding user response:

Don't. Get in the habit of writing async code in node everywhere, even if it doesn't need to be. It's just good practice, prevents refactoring a bunch of stuff in the future, and with modern JS it's simple to write:

import { mkdir, copyFile } from 'fs/promises';

async function createThing() {
    await mkdir('/path/to/dir');
    await copyFile(src, dest);
    ...
}

CodePudding user response:

Just use async/await for sequential, asynchronous operations. No need to write sync code that you'll regret later. Also, depending on the application, you might get trivial speedup opportunities by doing things concurrently.

  • Related