Async and await rewrite the read file function of node.js

tags: node.js  async  es6

const fs = require('fs');
const path = require('path');

function getFile() {
    return new Promise((resolve, reject) => {
        const filePath = path.resolve(path.join(__dirname, '../public/images'));
        fs.readdir(filePath, (err, files) => {
            var fileArr = [];
            files.forEach(filename => {
                fileArr.push('/static/images/' + filename);
                resolve(fileArr)
            });
        })   
    });
}

async function getImagesPath() {
    let result = await getFile();
    return result;
}


function insertDbData(){
    getImagesPath().then(res => {
        console.log(res)   
    })
}
insertDbData()


Intelligent Recommendation

Callback/Async Await/Promisify in Node.js

1. Callback function (Callback) 1. Concept 2. Example description Code description: Set a global variable A = 0, and then execute the WRITEFILE function (that is, write a file input01.txt). There is a...

async/await-1. Write an async/await function

async/await-1. Write an async/await function First quotenode-fetchLibrary, you can use await to request gracefully...

ES6 Async/Await function

Foreword     async-await ispromisewithgeneratoSyntactic sugar of r. Just to make our code more fluid, and of course enhance the readability of the code. simply put:async-await It i...

Async / await asynchronous function

Introduction to async Async is placed in front of the function as a keyword, indicating that the function is an asynchronous function, meaning that the function execution does not block the execution ...

async / await function

basic concepts The async and await seen command async / await syntactic sugar generator function, but there are some improvements to the generator function: generator is performed when the function re...

More Recommendation

Async/await function in ES6

async is called the ultimate asynchronous solution, avoiding callback hell, and processing asynchronous operations in a synchronous way async and Generator asyncYesGeneratorThe syntax sugar of the two...

Asynchronous function async and await

Synchronous function and asynchronous function The difference between the two Everyone can look at the link above, then I will describe the examples around you: The synchronization function is equival...

Async function + await

True to solve the problem of asynchronous callbacks, synchronous processes express asynchronous operation, essentially Async is Generator's syntax, async and await, used to optimize the use of Generat...

Async function and await

1.async function The async function does not return the return value. Default returns promise: {resolve} AWAIT is waiting is the result of promise execution success 2. The async function does not retu...

Asynchronous function -async / await

Asynchronous function --async / await The Async / AWAIT in ES8 is designed to solve the problem of using asynchronous structural organizational code. Async function The Async function returns a promis...

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

Top