打字稿 - 异步,等待和承诺不等待

时间:2021-04-04 10:10:23

标签: typescript async-await promise

我正在尝试连接到 Snowflake DB,但程序没有等待连接函数完成,尽管我使用了 async-await,但它仍在继续。

async function createConnection() {
try {
    return new Promise<any>(async (resolve, reject) => {
        // Create a Connection object that we can use later to connect.
        const connection = snowflake.createConnection({
            account: envs.account,
            username: envs.user,
            password: envs.password,
        }
        );
        // Try to connect to Snowflake, and check whether the connection was successful.
        connection.connect(
            (err, conn) => {
                if (err) {
                    console.error('Unable to connect: ' + err.message);
                }
                else {
                    console.log('Successfully connected to Snowflake.');
                }
            }
        );
        resolve(connection);
    })
}
catch (e) {
    console.error(e);
}
}

async function main() {

    const connection = await createConnection();
    if (connection == undefined || !connection?.isUp()) {
        console.error('Failed to connect to snowflake...');
        return;
    }

我得到的结果是:

Failed to connect to snowflake...
Successfully connected to Snowflake.

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

promise 应该解析为 <?php $allowedDomains = array('www.example1.com', 'www.example2.com', 'example3.com'); $referer = $_SERVER['HTTP_REFERER']; $domain = parse_url($referer); if(in_array( $domain['host'], $allowedDomains)) { $inputArray = array ( 'name' => 'TEST', 'version' => '1.0.0', 'download_url' => 'http://example.com', 'sections' => array ( 'description' => '', ), ); $encodedJSON = json_encode($inputArray, JSON_PRETTY_PRINT); print($encodedJSON); } else { echo "you are not allowed to post at this page"; exit(); } ?> ,您可以通过传入 conn 的回调访问该 connection.connect

// Doesn't have to be explicitly marked `async`, if you're not using `await` inside.
function createConnection() {
    return new Promise<any>((resolve, reject) => {
        // Create a Connection object that we can use later to connect.
        const connection = snowflake.createConnection({
            account: envs.account,
            username: envs.user,
            password: envs.password,
        });

        // Try to connect to Snowflake, and check whether the connection was successful.
        connection.connect(
            (err, conn) => {
                if (err) {
                    console.log(`Unable to connect: ${err.message}`);
                }
                else {
                    console.log('Successfully connected to Snowflake.');
                }

                // `conn` could be undefined, but your main function seems to handle that check...
                resolve(conn);
            }
        );
    });
}

async function main() {
    const connection = await createConnection();
    if (connection == undefined || !connection?.isUp()) {
        console.error('Failed to connect to snowflake...');
        return;
    }
}