ASYNC and AWAIT combine reading files

tags: ECMAScript  ES7  ASYNC and AWAIT read files  ASYNC and AWAIT operation files  ASYNC read files  AWAIT read files  

Previous article has been introduced, usepromiseThe way of reading files, greatly reduces the useTune hellDifficulties.

BelowasyncandawaitCombined with the read file instance, you can achieve the same effect:

// 1. Introduced FS module
const fs = require("fs");

 // Read "to learn"
function readWeiXue() {
    return new Promise((resolve, reject) => {
                 fs.readfile ("./ resources / for learning .md", (err, data) => {
                         // If you fail
            if (err) reject(err);
                         // If successful
            resolve(data);
        })
    })
}

function readChaYangShi() {
    return new Promise((resolve, reject) => {
                 fs.readfile ("./ resources /     .md", (err, data) => {
                         // If you fail
            if (err) reject(err);
                         // If successful
            resolve(data);
        })
    })
}

function readGuanShu() {
    return new Promise((resolve, reject) => {
                 fs.readfile ("./ Resources / View. MD", (Err, DATA) => {
                         // If you fail
            if (err) reject(err);
                         // If successful
            resolve(data);
        })
    })
}

 // Declare an ASYNC function
async function main(){
         // Get it for learning content
    let weixue = await readWeiXue();
         / / Get the content of the transplant
    let chayang = await readChaYangShi();
         / / Get a sense of reading
    let guanshu = await readGuanShu();

    console.log(weixue.toString());
    console.log(chayang.toString());
    console.log(guanshu.toString());
}

main();

readWeiXue()andreadChaYangShi()andreadGuanShu()All returned onepromiseObject;

awaitBe followed by one backpromiseObject function;

Intelligent Recommendation

Async and await

Talking about Async/Await Handling asynchronous with async/await Async and await async: Declare an asynchronous function (async function someName(){...}) Automatically convert regular functions intoPr...

async...await

ES8 introduces the async function, making asynchronous operations more convenient. Simply put, it is the syntactic sugar of the Generator function. Variants Asynchronous functions exist in the followi...

async await

Await means that the function can stop running, wait for the function in await to complete, and then continue to run the function instead of waiting for await, the function continues to run....

async-await

Async and await are used together 1Parallel processing 2Error handling Reprinted from: Note: async and await are used together, and the function after await is executed to execute the next js code. ex...

async await

Await is waiting for Promise's resolve and reject (axios can be packaged with axios actually Promise) Reprinted at: https://www.jianshu.com/p/e68e41fc402e...

More Recommendation

Storing adjacency matrix of FIG.

Reproduced in: https: //www.cnblogs.com/newwy/archive/2010/10/30/1865314.html...

When vue element ui uses the el-table component, the table will be disordered after the column is fixed

After using the fixed attribute, the fixed column and other columns are misaligned. When the fluid height and the left and right columns are fixed at the same time, this problem occurs when you slide ...

How to prohibit website access to language list in Windows 10 version 1803 or higher

How to prohibit website access to language list in Windows 10 version 1803 or higher Since the new version 1803 of Windwos10 is further UWP, the language setting block in the previous control panel ha...

Database SQL combat 1: Find all the information of the latest employees

thought: The topic asks to find all the information of the latest employees. Through a sub-query (select max(hire_date) from employees), find the latest hire time of the employee, hire_date, and then ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top