Dattiloscritto - attendere che la funzione di completare fino a quando l'esecuzione del seguente funzione

0

Domanda

Ho due funzioni: doAsyncStuff e doNextStep. Come posso proteggere doNextStep essere eseguito solo dopo doAsyncStuff è finito?

Ho provato qualcosa di simile, ma non mi ha aiutato: doAsyncStuff().then(_ => doNextStep());

async function doAsyncStuff() {
    if (vscode.workspace.workspaceFolders) {
        vscode.workspace.workspaceFolders.forEach(async (e) => {
            for (const [name, type] of await vscode.workspace.fs.readDirectory(e.uri)) {
                console.log(name);
            }
        });
    
    }
    return true;
}

function doNextStep() {
    console.log('next step');
}
async-await typescript
2021-11-23 19:10:06
1

Migliore risposta

0

Grazie mille per le vostre risposte, la soluzione di hotcakedev funziona out of the box. La promessa.tutti(vscode.area di lavoro.workspaceFolders.mappa(async ... aspettano))

E questa è come sembra (scenario risultante utilizza async funzione ricorsiva 'readFolders', che è anche lavorare bene):

        await Promise.all(vscode.workspace.workspaceFolders.map(async x => {


            for (const [name, type] of await vscode.workspace.fs.readDirectory(x.uri)) {
               console.log(name);
            }
            // await readFolders(x.uri,x.uri.path);



        }));



        console.log('fin');
2021-11-23 21:44:01

Contento che ha aiutato!
hotcakedev

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................