声明一个函数来等待一个诺言

时间:2018-12-11 23:20:33

标签: javascript promise

我具有以下功能:

@echo off
if "%~1"=="/LPipe" goto :/LPipe
if "%~1"=="/RPipe" goto :/RPipe

set /p user_id=Username:
set /p pwd=Password:

"%~f0" /LPipe | "%~f0" /RPipe
exit /b


:/LPipe
REM This will be executed inside a pipe but in batch context

REM Enable delayed expansion to be able to send any special characters
setlocal EnableDelayedExpansion

REM It's easy to take care of trailing spaces, no special hacks needed.
echo !user_id!
echo !pwd!
goto :EOF


:/RPipe
REM This will be executed inside a pipe but in batch context

%install_dir%\vpncli.exe connect myvpn.mydomain.TLD -s
goto :EOF

我希望能够运行此程序,但是如果要进行解压缩调用,则要等待它。我目前正在promise链中调用此函数,它不需等待即可返回链中,并在上一个promise链完成后返回。

1 个答案:

答案 0 :(得分:3)

您可以将该逻辑与Promise函数一起放入async

function JSON_to_buffer(json) {
    return new Promise(function (resolve, reject) {
        let buff = Buffer.from(json);
        if (json.length < constants.min_draft_size_for_compression) {
            return resolve(buff);
        }

        zlib.deflate(buff, (err, buffer) => {
            if (!err) {
                resolve(buffer);
            } else {
                reject(err);
                /*return BPromise.reject(new VError({
                    name: 'BufferError',
                }, ));*/
            }
        });

    });

}

async function main() {
    try {
        let buffer = await JSON_to_buffer(myJSON);
    } catch (e) {
        console.log(e.message);
    }
}