简体   繁体   中英

I want to return function after doing mapSeries

I want to return querys(function) after doing mapSeries

how do i this? please help me!

async querys(querys) {
        const pool = await poolPromise;
        if (pool != null) {
            const transaction = new sql.Transaction(pool);
            
            await new Promise(resolve => transaction.begin(resolve));
            const request = new sql.Request(transaction)
    
            try {
                let results = []
                // ------------- Series -------------
                async.mapSeries(querys, async (query, callback) => {
                                console.log('----------')
                                const result = await request.query(query);
                                // return result.recordset;
                                results.push(result.recordset);
                }, async (err) => {
                    if (err) {
                        throw new Error(err);
                    }
                    else {
                        await transaction.commit();
                    }
                });
                console.log('hello');
                // -------------- return ---------------
                return [true, results];
            } catch(err) {
                await transaction.rollback();
                return [false, err];
            }
        }
        else {
            return [false, 'connect error'];
        }
    },

querys

expected output : [true, [object], [object] ... ]

real output : [true, []]

self answer

added promise

async querys(querys) {
        return new Promise( async resolve => {
            const pool = await poolPromise;
            if (pool != null) {
                const transaction = new sql.Transaction(pool);
                
                await new Promise(resolve => transaction.begin(resolve));
                const request = new sql.Request(transaction)
        
                try {
                    let results = []
                    async.mapSeries(querys, async (query, callback) => {
                                    const result = await request.query(query);
                                    results.push(result.recordset);
                    }, async (err) => {
                        if (err) {
                            throw new Error(err);
                        }
                        else {
                            console.log('commit')
                            await transaction.commit();
                            resolve([true, results]);
                        }
                    });
                } catch(err) {
                    await transaction.rollback();
                    resolve([false, err]);
                }
            }
            else {
                resolve([false, 'connect error']);
            }
        });
    },

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