简体   繁体   中英

Impact of sync function inside async functions

Lets imagine an asynchronous function that loads a file first and does something asynchronously with it afterwards. The function can't continue without the file, so my assumption is that loading this file could be done synchronously (*):

const asyncFnWithSyncCode(filePath, next) {

    // Load file
    const file = fs.readFileSync(filePath)

    // Continue to process file with async functions
    // ...

    next(null, processedFile)

}

asyncFnWithSyncCode could be called several times for different files:

async.parallel([
   (done) => { asyncFnWithSyncCode('a.json', done) },
   (done) => { asyncFnWithSyncCode('b.json', done) },
   (done) => { asyncFnWithSyncCode('c.json', done) }
], next)

My question is: How does this impact the performance? Will the sync function cause the other readFileSync s to be delayed? Will it have an impact at all?

Best-practices, resources and opinions are welcome. Thanks!

(*) I know that I could simply use the async readFile -version, but I would really like to know how it works in this special construction.

Will the sync function cause the other readFileSyncs to be delayed?

Yes. NodeJS runs all of your JavaScript code on a single thread, using an event loop (job queue), which is one of the reasons that using asynchronous system calls is strongly encouraged over synchronous ones.

readFile schedules the read operation and then lets other things happen on the JavaScript thread while the I/O layer is waiting for the data to come in; Node's I/O layer queues a task for the JavaScript thread when data is available, which is what ultimately makes your readFile callback get called.

In contrast, readFileSync holds up that one single JavaScript thread, waiting for the file data to become available. Since there's only one thread, that holds up everything else your code might otherwise be doing, including other readFileSync calls.

Your code doesn't need to use readFileSync (you almost never do); just use readFile 's callback:

const asyncFnWithSyncCode(filePath, next) {

    // Load file
    fs.readFile(filePath, function(err, file) {
        if (err) {
            // ...handle error...
            // ...continue if appropriate:
            next(err, null);
        } else {
            // ...use `file`...

            // Continue to process file with async functions
            // ...
            next(null, processedFile);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM